The 4th "intelligence Cup" National College Students' IT skills competition (group B of the preliminary competition) - problem solution

Posted by Spiz on Mon, 20 Dec 2021 13:15:42 +0100

The 4th "intelligence Cup" National College Students' IT skills competition (group B of the preliminary competition)

Original score of group A

Simulation, and then the final result can be converted to int.
AC Code:

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);

//    cin.ordinaryChars('0', '9') ;
//    cin.wordChars('0', '9');

    public static void main(String[] args) throws Exception {

        int t= nextInt();
        int h = nextInt();
        int e = nextInt();
        System.out.println((int)(t*0.2+h*0.3+e*0.5));

        closeAll();
    }


    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

B. report scoring

Just simulate.

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);

//    cin.ordinaryChars('0', '9') ;
//    cin.wordChars('0', '9');

    public static void main(String[] args) throws Exception {

        int t= nextInt();
//        int h = nextInt();
//        int e = nextInt();
//        System.out.println(String.format("%.0f",t*0.2+h*0.3+e*0.5));
        while(t--!=0){
            int a = nextInt();
            int p = nextInt();
            if(p<16){
                a-=10;
            }
            if(p>20){
                a-=(p-20);
            }
            if(a<=0) a =0;
            System.out.println(a);
        }

        closeAll();
    }


    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

C competition score

Simulation.

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);

//    cin.ordinaryChars('0', '9') ;
//    cin.wordChars('0', '9');

    public static void main(String[] args) throws Exception {

        int n= nextInt();
        int[] a = new int[n+10];
        int mind=2000,maxd=0;
        for(int i=1;i<=n;++i){
            a[i]=nextInt();
            mind=Math.min(a[i],mind);
            maxd=Math.max(a[i],maxd);
        }
        for(int i=1;i<=n;++i){
            a[i]=(int)(100*((double)(a[i]-mind)/(maxd-mind)));
        }
        for(int i=1;i<=n;++i){
            if(i==1) System.out.print(a[i]);
            else System.out.print(" "+a[i]);
        }
        System.out.println();
        closeAll();
    }


    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

D small card and prime 2

subject

Title Link

Find out how many y can make xy=z (exclusive or operation in the content of this blog), and Y < X. where z is a prime number (prime number).

📢 Tip: the solution of this problem is for reference only. It's too delicious. It can only be written like this...

prescience

Binary length of 2.0 number

Logarithms can be used to calculate log2 (N). However, there are only log (double) (e-based), log10 (double) and other functions in java. At this time, log2 (N) can be realized by using the bottom changing formula. There are log2 (x) functions in C + +.

log2N=logeN/loge2, logeN represents the logarithm of N with e as the base, and loge2 represents the logarithm of 2 with e as the base.

There is also a simple method in Java:

System.out.println(Integer.toBinaryString(2).length());
System.out.println(Integer.toBinaryString(3).length());
System.out.println(Integer.toBinaryString(5).length());
System.out.println(Integer.toBinaryString(7).length());
System.out.println(Integer.toBinaryString(11).length());

System.out.println((int) (Math.log(2)/Math.log(2)));
System.out.println((int) (Math.log(3)/Math.log(2)));
System.out.println((int) (Math.log(5)/Math.log(2)));
System.out.println((int) (Math.log(7)/Math.log(2)));
System.out.println((int) (Math.log(11)/Math.log(2)));

📢 Note: the results calculated by the above two methods are different, with a difference of 1. Using the first method to find the binary bit length of the array needs to be reduced by 1. Using the second method to find the binary bit of the number does not need to be reduced by 1. As for why, I don't know. Wait for the official solution later. Maybe it's because if the decimal represents that they haven't reached a higher level, it's still the i-th place.

2.1 topic meaning conversion

Because xy=z, y=xz. So you can find y by prime and given x.

📢 Tip: you can directly watch the process demonstration first.

2.2 binary (key)

We need to know which bit of the highest bit (rightmost) 1 of the binary bits of a number is in. For example:

3 - > 11, 2 bits. (10)

7 - > 111, 3 bits. (100)

11 - > 1011, 4 bits. (1000)

This is for prime numbers. If the binary of a prime number T is 1010101, all we need is the position of its highest 1. After processing, the prime number is 1000000.

The advantage of this processing is that all prime numbers between 1000000 and 1111111 can be represented in this way. And when we find a prime greater than 1000000, Just add the number of primes of this length (so you can use an array cnt[i]) To represent the number of binary numbers of prime numbers with length I starting with 1). Of course, this calculation only represents all prime numbers between 1000000 and 1111111; We also need to find all prime numbers between 100000 and 111111, all prime numbers between 10000 and 11111,..., all prime numbers between 10 and 11, and all prime numbers between 1 and 1.

But in this problem, we find y less than x. And y=x^z, so,

2.3 prime range

The range of X given in the title is 1 * 106. And because y=x^z. Therefore, prime numbers may exist outside the X range, so you need to find a larger range when looking for prime numbers. Just double it.

2.4 principle (supplementary)

We need to know first:

The result of 1000 ^ 1xxx must be less than 1000.

Another example: the result of 10010 ^ 1xxxx must be less than 10010.

After the conversion, the title is required to be y=x^z. Where x is known and z is prime (you can find it yourself).

If the binary of X is: 1, we operate: 1 ^ 1. The result must be less than x.

If the binary bit of X is 11, the operation is 11 ^ 10. The result must be less than x.

If the binary bit of X is 111, the operation is 111 ^ 100. The result must be less than x.

If the binary bit of X is 1111, the operation is 1111 ^ 1000. The result must be less than x.

...

But this time, we only found the part of the highest 1. It is also mentioned in 2.2.

Process demonstration

Example 1: x=10.

  1. Let's find the prime first. Expressed by cnt[i]: how many prime binary numbers have a length of I.

Prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23.

Binary numbers are: (1 is not prime, so cnt[1]=0)

2: 10 -> 10 cnt[2]

3: 11 -> 10 cnt[2]

5: 101 -> 100 cnt[3]

7: 111 -> 100 cnt[3]

11: 1011 -> 1000 cnt[4]

13: 1101 -> 1000 cnt[4]

17: 10001 -> 10000 cnt[5]

19: 10011 -> 10000 cnt[5]

23: 10111 -> 10000 cnt[5]

...

  1. Then convert x=10 to binary: 1010

Then we calculate from right to left (low to high): (we only calculate that the binary bit is 1)

The second is 1. Our final result is ans+=cnt[2];

The fourth is 1. Our final result is ans+=cnt[4];

Final results: 4

Example 2: x=9

  1. The first step is the same as example 1 to find all prime numbers.

  2. Then convert x=9 to binary: 1001

Here, the first and fourth bits are 1, so ans can add cnt[1] and cnt[4] respectively, and the final result is 2.

AC code

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);


    public static int[] cnt = new int[2000000];
    public static void main(String[] args) throws Exception {

        for(int i=2;i<=1500000;++i){
            if(isPrime_3(i)){
                cnt[Integer.toBinaryString(i).length()-1]++;
            }
        }
        int t = nextInt();
        while(t--!=0){
            int ans = 0;
            int ma = nextInt();
            for(int i=0;i<=20;++i){
                if(((ma>>i)&1)==1){
                    ans+=cnt[i];
                }
            }
            cout.println(ans);
            cout.flush();
        }
        closeAll();
    }
    public static boolean isPrime_3( int num )
    {
        if(num ==2|| num==3 )
            return true ;
        if(num %6!= 1&&num %6!= 5)
            return false;
        int tmp = (int) Math.sqrt( num);
        for(int i= 5;i <=tmp; i+=6 )
            if(num %i== 0||num %(i+ 2)==0 )
                return false ;
        return true ;
    }

    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);

    public static int[] cnt = new int[2000000];
    public static void main(String[] args) throws Exception {

        for(int i=2;i<=1500000;++i){
            if(isPrime_3(i)){
                cnt[(int) (Math.log(i)/Math.log(2))]++;
            }
        }

        int t = nextInt();
        while(t--!=0){
            int ans = 0;
            int ma = nextInt();
            for(int i=0;i<=20;++i){
                if(((ma>>i)&1)==1){
                    ans+=cnt[i];
                }
            }
            cout.println(ans);
            cout.flush();
        }
        closeAll();
    }
    public static boolean isPrime_3( int num )
    {
        if(num ==2|| num==3 )
            return true ;
        if(num %6!= 1&&num %6!= 5)
            return false;
        int tmp = (int) Math.sqrt( num);
        for(int i= 5;i <=tmp; i+=6 )
            if(num %i== 0||num %(i+ 2)==0 )
                return false ;
        return true ;
    }

    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

E

Adjacency matrix.

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @Ink memory www.tothefor.com com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));

//    cin.ordinaryChars('0', '9') ;
//    cin.wordChars('0', '9');

    public static void main(String[] args) throws Exception {

        int n = nextInt();
        int k = nextInt();
        Vector<Integer>[] edge = new Vector[1005];

        for(int i=1;i<=k;++i){
            edge[i] = new Vector<>();
        }

        for (int i = 1; i <= n; ++i) {
            int a = nextInt();
            if (a == 1) {
                int p = nextInt();
                for (int j = 1; j <= p; ++j) {
                    int c1, v1;
                    c1 = nextInt();
                    v1 = nextInt();
                    edge[c1].add(v1);
                }
            } else if (a == 2) {
                int ans = 0;
                int x, mind, maxd;
                x = nextInt();
                mind = nextInt();
                maxd = nextInt();
                int len = edge[x].size();
                for (int j = 0; j < len; ++j) {
                    if (edge[x].get(j) >= mind && edge[x].get(j) <= maxd) ans++;
                }
                cout.println(ans);
                cout.flush();
            }
        }

        closeAll();
    }


    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

Topics: Java Back-end test