2023 14th Lanqiao Cup JavaA group real question solution, the code has passed the test platform test

GHIJ has not been written yet

[Special Date for Question A] 

 [Analysis of ideas]

Simple brute force loop solution

【Answer】

35813063

【Code】

/**
 * @ProjectName: study3
 * @FileName: A
 * @author:HWJ
 * @Data: 2023/9/4 7:46
 * // 1980
 */
public class A {
    public static void main(String[] args) {
        int[] days = {0,31, 28,31,30,31,30,31,31,30,31,30,31};
        int[] days2 = {0,31, 29,31,30,31,30,31,31,30,31,30,31};
        long total = 0;
        for (int i = 2000; i < 2000000; i++) {
            for (int j = 1; j <= 12; j++) {
                if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0){
                    for (int k = 1; k <= days2[j]; k++) {
                        if (i % j == 0 && i % k == 0){
                            total++;
                        }
                    }
                }else {
                    for (int k = 1; k <= days[j]; k++) {
                        if (i % j == 0 && i % k == 0){
                            total++;
                        }
                    }
                }
            }
        }
        System.out.println(++total);
    }
}

 [B: AND or XOR]

[Analysis of ideas]

A simple brute force recursive solution

【Answer】

30528

【Code】

/**
 * @ProjectName: study3
 * @FileName: B
 * @author:HWJ
 * @Data: 2023/9/4 8:04
 */
public class B {
    public static int ans = 0;
    public static int[][] map = new int[6][6];
    public static void main(String[] args) {
        map[5] = new int[]{0,1,0,1,0,1};
        dfs(1, 5);
        System.out.println(ans);
    }
    public static void dfs(int i, int n){
        if (n == 1){
            if (map[1][1] == 1){
                ++ans;
            }
        }else {
            if(i < n){
                map[n-1][i] = map[n][i] & map[n][i+1];
                dfs(i + 1, n);

                map[n-1][i] = map[n][i] | map[n][i+1];
                dfs(i + 1, n);

                map[n-1][i] = map[n][i] ^ map[n][i+1];
                dfs(i + 1, n);
            }else {
                dfs(1, n-1);
            }
        }
    }
}

[C: Average]

Question description

There is an array of length n (n is a multiple of 10), and each number ai is an integer in the interval [0, 9]. Xiao Ming found that the number of occurrences of each number in the array was uneven, and the cost of changing the i-th number was bi. He wanted to change the values ​​of several numbers so that the number of occurrences of these 10 numbers was equal (all equal to n/10). What is the minimum sum of the costs?

Input format

The first line of input contains a positive integer n.

The next n lines, the i-th line contains two integers ai, bi, separated by a space.

Output format

The output line contains a positive integer representing the answer.

Sample input

10
1 1
1 2
1 3
2 4
2 5
2 6
3 7
3 8
3 9
4 10

Sample output

27

hint

Changing only the 1st, 2nd, 4th, 5th, 7th, and 8th numbers will cost 1 + 2 + 4 + 5 + 7 + 8 = 27.

For 20% of the evaluation cases, n ≤ 1000;

For all evaluation cases, n ≤ 100000, 0 < bi ≤ 2 × 10^5 

Analysis of ideas

Because each number can only be [0,9], and the number must be n/10, we can construct ten arrays (array size is n/10) to store the ten numbers separately. If the modified number is not full in the ten arrays, it will be stored directly. If it is full, the array will be sorted, and then the current b will be compared with the first number in the array, and the larger number will be stored in the array. Then total += small number. This way we can find the minimum cost.

Code

import java.util.Arrays;
import java.util.Scanner;

/**
 * @ProjectName: study3
 * @FileName: C
 * @author:HWJ
 * @Data: 2023/9/4 8:33
 */
public class C {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        long total = 0;
        int n = input.nextInt();
        int l = n / 10;
        int[] m0 = new int[l];
        int[] m1 = new int[l];
        int[] m2 = new int[l];
        int[] m3 = new int[l];
        int[] m4 = new int[l];
        int[] m5 = new int[l];
        int[] m6 = new int[l];
        int[] m7 = new int[l];
        int[] m8 = new int[l];
        int[] m9 = new int[l];
        int n0 = 0;
        int n1 = 0;
        int n2 = 0;
        int n3 = 0;
        int n4 = 0;
        int n5 = 0;
        int n6 = 0;
        int n7 = 0;
        int n8 = 0;
        int n9 = 0;
        for (int i = 0; i < n; i++) {
            int a = input.nextInt();
            int b = input.nextInt();
            if(a == 0){
                if(n0 < l){
                    m0[n0++] = b;
                }else {
                    Arrays.sort(m0);
                    if(b >= m0[0]){
                        total += m0[0];
                        m0[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 1){
                if(n1 < l){
                    m1[n1++] = b;
                }else {
                    Arrays.sort(m1);
                    if(b >= m1[0]){
                        total += m1[0];
                        m1[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 2){
                if(n2 < l){
                    m2[n2++] = b;
                }else {
                    Arrays.sort(m2);
                    if(b >= m2[0]){
                        total += m2[0];
                        m2[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 3){
                if(n3 < l){
                m3[n3++] = b;
                }else {
                    Arrays.sort(m3);
                    if(b >= m3[0]){
                        total += m3[0];
                        m3[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 4){
                if(n4 < l){
                    m4[n4++] = b;
                }else {
                    Arrays.sort(m4);
                    if(b >= m4[0]){
                        total += m4[0];
                        m4[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 5){
                if(n5 < l){
                    m5[n5++] = b;
                }else {
                    Arrays.sort(m5);
                    if(b >= m5[0]){
                        total += m5[0];
                        m5[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 6){
                if(n6 < l){
                    m6[n6++] = b;
                }else {
                    Arrays.sort(m6);
                    if(b >= m6[0]){
                        total += m6[0];
                        m6[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 7){
                if(n7 < l){
                    m7[n7++] = b;
                }else {
                    Arrays.sort(m7);
                    if(b >= m7[0]){
                        total += m7[0];
                        m7[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 8){
                if(n8 < l){
                    m8[n8++] = b;
                }else {
                    Arrays.sort(m8);
                    if(b >= m8[0]){
                        total += m8[0];
                        m8[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
            if(a == 9){
                if(n9 < l){
                    m9[n9++] = b;
                }else {
                    Arrays.sort(m9);
                    if(b >= m9[0]){
                        total += m9[0];
                        m9[0] = b;
                    }else {
                        total += b;
                    }
                }
            }
        }
        System.out.println(total);
    }
}

【D: chessboard】

Question description

Xiaolan has a chessboard of n × n size. At the beginning, the chessboard is full of white pieces. Xiaolan performed m operations, and each operation would invert the colors of all the chess pieces within a certain range on the chessboard (that is, the white chess pieces turned into black, and the black chess pieces turned into white). Please output the color of each piece on the chessboard after all operations are completed.

Input format

The first line of input contains two integers n, m, separated by a space, indicating the size of the chessboard and the operand.

Each of the next m lines contains four integers x1, y1, x2, y2. Adjacent integers are separated by a space, indicating that the color of the chess pieces in the x1 to x2 rows and y1 to y2 columns will be inverted.

Output format

Output n lines, with n 0s or 1s in each line indicating the color of the chess piece at that position. If it is white, output 0, otherwise output 1.

Sample input

3 3
1 1 2 2
2 2 3 3
1 1 3 3

Sample output

001
010
100

hint

For 30% of the evaluation cases, nm ≤ 500;

For all evaluation cases, 1 ≤ n, m ≤ 2000, 1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m.

 [Analysis of ideas]

Create a chessboard, initialize the chessboard to 0, and then negate it when it needs to be negated. This logic is very simple, but because the output volume is large, we need to reduce the output time to prevent the run timeout. PrintWriter is required.

【Code】

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * @ProjectName: study3
 * @FileName: D
 * @author:HWJ
 * @Data: 2023/9/4 9:09
 */
public class D {
    public static int[][] map;
    // 这样的输出更节约时间,在输出量大导致运行时间超时的时候,可以用这样的方法来解决
    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        map = new int[n][n];
        for (int i = 0; i < m; i++) {
            int x1 = input.nextInt();
            int y1 = input.nextInt();
            int x2 = input.nextInt();
            int y2 = input.nextInt();
            reverse(x1, y1, x2, y2);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                pw.print(map[i][j]);
            }
            pw.println();
        }
        pw.flush();
    }

    public static void reverse(int x1, int y1, int x2, int y2){
        for (int j = x1 - 1; j <= x2 - 1; j++) {
            for (int k = y1 - 1; k <= y2 - 1; k++) {
                if(map[j][k] == 1){
                    map[j][k] = 0;
                }else {
                    map[j][k] = 1;
                }
            }
        }
    }
}

[E: Number of coprime numbers]

Question description

Given a and b, find how many x's are relatively prime to ab in 1 ≤ x < ab. Since the answer can be very large, you only need to output the answer modulo 998244353.

Input format

Input a line containing two integers representing a and b respectively, separated by a space.

Output format

One line of output contains an integer representing the answer.

Sample input

2 5

Sample output

16

hint

For 30% of the evaluation cases, ab ≤ 106;

For 70% of the evaluation cases, a ≤ 106, b ≤ 109;

For all evaluation cases, 1 ≤ a ≤ 109, 1 ≤ b ≤ 1018.

 Analysis of ideas

Here you need to use the Euler function.

Code

/**
 * @ProjectName: study3
 * @FileName: E
 * @author:HWJ
 * @Data: 2023/9/4 10:15
 */
public class E {
    static int mod = 998244353;

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        long a = input.nextLong();
        long b = input.nextLong();
        long m = 1;
        long k = b;
        for (; k >= 1; k >>= 1, a = a * a % mod) {
            if ((k & 1) == 1) {
                m = m * a % mod;
            }
        }
        System.out.println(oula(m));

    }

    public static long oula(long num) {
        long total = num;
        for (long i = 2; i < num; i++) {
            if (num % i == 0) {
                while (num % i == 0) {
                    num /= i;
                }
                total = total - total / i;
            }
        }
        if(num > 1){
            total = total - total / num;
        }
        return total;
    }


}

[F: sum of factorials]

Question description

Given n numbers Ai, ask what is the largest m that can satisfy m! as a factor of ∑ni=1(Ai!). where m! represents the factorial of m, that is, 1 × 2 × 3 × · · · × m.

Input format

The first line of input contains an integer n.

The second line contains n integers, representing Ai respectively, and adjacent integers are separated by a space.

Output format

One line of output contains an integer representing the answer.

Sample input

3
2 2 2

Sample output

3

hint

For 40% of the evaluation cases, n ≤ 5000;
for all evaluation cases, 1 ≤ n ≤ 105 1 ≤ Ai ≤ 109.

Analysis of ideas

For a factorial, if n > m, then n! It must be a multiple of m!, so for the sum of the given n numbers Ai, their sum must be a multiple of min(Ai). So we need to find this minimum maxV. When it reaches maxV + 1, it can become (maxV + 1)!, and we need to simulate the process of this factorial sum.

Code

import java.util.HashMap;
import java.util.Scanner;

/**
 * @ProjectName: study3
 * @FileName: F
 * @author:HWJ
 * @Data: 2023/9/4 19:19
 */
public class F {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        int maxV = 1000000000;
        for (int i = 0; i < n; i++) {
            int a = input.nextInt();
            maxV = Math.min(maxV, a);
            if (!map.containsKey(a)) {
                map.put(a, 1);
            } else {
                map.put(a, map.get(a) + 1);
            }
            while (map.get(a) % (a + 1) == 0) {
                if (map.containsKey(a + 1)){
                    map.put(a + 1, map.get(a + 1) + map.get(a) / (a + 1));
                }else {
                    map.put(a + 1, map.get(a) / (a + 1));
                }

                map.put(a, 0);
                if (maxV == a) {
                    maxV = a + 1;
                }

                a++;
            }
        }
        System.out.println(maxV);
    }
}

 [G: Xiaolan’s travel plan]

Question description

Xiaolan is planning a long trip. Xiao Lan plans to complete the trip by car. Apparently he needed to refuel along the way, otherwise he might not have been able to complete the trip.

Xiaolan needs to pass through n locations in sequence, and it takes Disi liters of fuel to reach the i-th location from the i − 1 location. There is a gas station at every location Xiao Lan passes, but the regulations at each gas station are also different. It costs Costi to fill 1 liter of oil at the i-th gas station, and only Limi liters of oil can be filled at most at this gas station.

The fuel tank of Xiaolan's car also has a capacity limit. His car can only carry m liters of fuel at most. 

Xiao Lan’s gas tank is full at the beginning. How much money does Xiao Lan need to prepare to successfully complete his travel plan? If Xiaolan cannot complete his travel plan under the given conditions no matter how much money he prepares, please output −1.

Input format

The first line of input contains two integers nm separated by a space. 

Each of the next n lines contains 3 integers Disi Costi Limi, with a space separating adjacent integers. 

Output format

One line of output contains an integer representing the answer. 

Sample input

copy

4 5
2 9 2
4 5 6
3 2 2
4 1 3

Sample output

copy

38

hint

For 30% of the evaluation cases, n Disi Costi Limi m ≤ 300;

For 60% of the evaluation cases, n Disi Costi Limi m ≤ 5000; 

For all evaluation cases, 1 ≤ n ≤ 2 × 105, 1 ≤ Disi Limi m ≤ 109, 1 ≤ Costi ≤ 40000. 

[H: Sun]

Question description

On this day, Xiaolan placed a sun on the point (X, Y) of the two-dimensional coordinate system and regarded it as a point light source.

He took n line segments and placed them parallel to the x-axis in the coordinate system. The left endpoint of the i-th line segment is at xi, yi, and the length is li. Line segments will not overlap or partially overlap (but endpoint intersections may occur). Xiaolan wants to know how many line segments can be illuminated by the sun (even if a line segment has a length greater than 0, it is illuminated)

Input format

The first line of input contains three positive integers n, X, Y. Adjacent integers are separated by a space.

The next n lines, the i-th line contains three integers xi, yi, li, with a space separating adjacent integers.

Output format

The output line contains a positive integer representing the answer.

Sample input

copy

3 10 2000000
5 3 5
6 2 4
0 1 10

Sample output

copy

2

hint

The first line segment is illuminated at the top, the second line segment is completely blocked by the first line segment, and the left segment of the third line segment can be illuminated.

For 30% of the evaluation cases, n ≤ 1000;

For all evaluation cases, 1 ≤ n ≤ 100000, 0 ≤ xi , X ≤ 107 , 0 < yi ≤ 105 , 0 < li ≤ 100, 106 < Y ≤ 107 .

[I: Tower]

Question description

Xiaolan is playing a tower climbing game. The number of floors of the tower is unlimited, but the game only lasts at most n turns.

Xiao Lan has m points of energy at the beginning, and in each round there is a value Ai representing Xiao Lan's character status. Xiao Lan can choose to consume any point of energy Ci each round (the minimum consumption is 1 point, there is no upper limit). In this round, he can climb up to the Ai·Ci level. The actual number of floors climbed depends on Xiao Lan's own performance in this round, but at worst it will climb up one floor.

When Little Blue's energy points are exhausted in a certain round, the game ends after completing this round. n After the round is over, the game will end directly regardless of whether there is any energy left.

Give Xiaolan's Ai for each round and his energy points m at the beginning. Xiao Lan wants to know how many different possible gameplays there are. If Xiao Lan spends different energy points in any corresponding round in the two play-throughs or ends at a different level at the end of the round, then the two play-throughs are considered different.

Input format

The first line of input contains two integers n, m, separated by a space.

The second line contains n integers Ai, separated by a space between adjacent integers, indicating Xiaolan's status value in each round.

Output format

The output line contains an integer representing the number of different play sessions under the given conditions. Since the answer can be very large, you only need to output the answer modulo 998244353.

Sample input

copy

9 15
3 2 5 7 1 4 6 8 3

Sample output

copy

392149233

hint

For 40% of the evaluation cases, n ≤ 300, m ≤ 500;

For all evaluation cases, 1 ≤ n ≤ 2 × 105, n ≤ m ≤ 1018, and 1 ≤ Ai ≤ 109.

[J: Reverse XOR 01 string]

Question description

Initially, there is an empty 01 string, and each operation can add 0 or 1 to the left or right side. You can also perform an inverse XOR operation on the entire string: take s ′ = s ⊕ rev(s), where s is the current 01 string, ⊕ means bitwise XOR, rev(s) means flipping s, that is to say, take Center position and swap all symmetrical characters of the two positions. For example, rev(0101) = 1010 rev(010) = 010 rev(0011) = 1100.

The anti-XOR operation can be used at most once (it may not be used or it may be used once).

Given a 01 string T, ask how many 1's need to be added at least to get T from an empty 01 string.

In this question, any number of 0s can be added.

Input format

The input line contains a string of 01's representing the given T.

Output format

One line of output contains an integer, indicating the minimum number of 1's that need to be added.

Sample input

copy

00111011

Sample output

copy

3

hint

For 20% of the evaluation cases, |T| ≤ 10;

For 40% of the evaluation cases, |T| ≤ 500;

For 60% of the evaluation cases, |T| ≤ 5000;

For 80% of the evaluation cases, |T| ≤ 105;

For all evaluation cases, 1 ≤ |T| ≤ 106, it is guaranteed that T only contains 0 and 1.

Guess you like

Origin blog.csdn.net/weixin_73936404/article/details/132591701