2022-02-16 swipe questions and punch in every day

Posted by Formula on Wed, 16 Feb 2022 12:59:32 +0100

1, ACWing:900 Integer partition

(1) Problem description

A positive integer nn can be expressed as the sum of several positive integers, such as: n=n1+n2 +... + nkn=n1+n2 +... + nk, where n1 ≥ n2 ≥... ≥ nk,k ≥ 1n1, n1 ≥ n2 ≥... ≥ nk,k ≥ 1.

We call such a representation a partition of positive integer nn.

Now, given a positive integer nn, please find out how many different partition methods nn has.

Input format

A total of one line, including an integer nn.

Output format

A total of one line, including an integer, indicating the total partition quantity.

Since the answer may be very large, please take the module of 109 + 7109 + 7 for the output result.

Data range

1≤n≤10001≤n≤1000

Input sample:

5

Output example:

7

(2) Code implementation

        

import java.util.Scanner;
/**
 * First, assign 1 to f[0]
 * Equivalent to f[0][0~n] = 1. Since it is a rolling array, only one can be set
 * 
 * State transition:
 *      f[i][j] = f[i-1][j]+f[i-1][j-i]+f[i-2][j-2*i]+...
 *      f[i][j]:Select from 1~i the number of schemes whose total is j
 *      Optimization equation:
 *      1. f[i][j] = f[i-1][j]+f[i][j-i];
 *      2. f[j] = f[j]+f[j-i];
 */
public class Main{
    static int N = 1010;
    static int f[] = new int[N];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        f[0] = 1;
        for(int i = 1; i <= n ; i ++) {
            for(int j = i; j <= n; j ++) {
                f[j] = (int) ((f[j]+f[j-i])%(1e9+7));
            }
        }
        System.out.println(f[n]);
    }
}

2, ACWing: 338 Counting problem

(1) Problem description

        

Given two integers ∼ aa ∼ and ∼ bb, find the number of occurrences of ∼ 0 ∼ 90 ∼ 9 ∼ in all numbers between ∼ aa ∼ and ∼ bb.

For example, if a=1024, b=1032a=1024, b=1032, then there are 99 , between , aa , and , bb , as follows:

1024 1025 1026 1027 1028 1029 1030 1031 1032

Among them, 0 , 1010 , 1 , 1010 , 2 , 77 , 3 , 33 , and so on

Input format

The input contains multiple sets of test data.

Each group of test data occupies one line, including two integers # aa # and # bb.

When 0 {is entered, the processing is not terminated.

Output format

Each group of data outputs a result, and each result occupies one row.

Each result contains ten numbers separated by spaces. The first number represents the number of occurrences of "0", the second number represents the number of occurrences of "1", and so on.

Data range

0<a,b<1000000000<a,b<100000000

Input sample:

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

Output example:

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

(2) Code implementation

        

import java.io.*;
import java.util.*;
class Main{
    static int get(List<Integer> list, int l, int r){
        int res = 0;
        for(int i=l; i>=r; i--){
            res = res * 10 + list.get(i);
        }
        return res;
    }

    static int power10(int x){
        int res = 1;
        while(x!=0){
            res *= 10;
            x--;
        }
        return res;
    }
    static int count(int n, int x){
        if(n==0) return 0; //If n is 0, just return 0 directly
        List<Integer> l = new ArrayList<Integer>();

        //Add each bit of n to l
        while(n!=0){
            l.add(n % 10);
            n /= 10;
        }
        int len = l.size();

        //For 0, you do not need to enumerate the highest bit
        int res = 0;
        for(int i=len-1-(x==0?1:0); i>=0; i--){
            if(i < len-1){
                res += get(l, len-1, i+1) * power10(i);
                if(x == 0) res -= power10(i);
            }

            if(l.get(i) > x) res += power10(i);
            else if(l.get(i) == x) res += get(l, i-1, 0) + 1;
        }

        return res;
    }

    public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String[] arr = in.readLine().split(" ");
        int a = Integer.parseInt(arr[0]);
        int b = Integer.parseInt(arr[1]);

        while(a!=0 || b!=0){
            if(a > b){
                int tmp = a;
                a = b;
                b = tmp;
            }
            for(int i=0; i<=9; i++){
                System.out.print(count(b, i)-count(a-1, i)+" ");
            }
            System.out.println();

            String[] cur = in.readLine().split(" ");
            a = Integer.parseInt(cur[0]);
            b = Integer.parseInt(cur[1]);
        }
    }
}

III. Blue Bridge Cup 2019 question 9: suffix expression

(1) Problem description

        

Given n plus signs, M minus signs and N + M +1 integers A1,A2, ···, AN+M+1, Xiao Ming wants to know which of all the legal suffix expressions rounded up by N plus signs, M minus signs and N + M +1 integers has the largest result?

Please output this maximum result.

For example, if 1 2 3 + -, the result of the suffix expression "2 3 + 1 -" is 4, which is the largest.

[input format]

The first line contains two integers N and M. The second line contains N + M + 1 integers A1,A2, ···, AN+M+1.

[output format]

Output an integer representing the answer.

[sample input]

1 1
1 2 3

[sample output]

4

[evaluation case scale and agreement]

For all evaluation cases, 0 ≤ N,M ≤ 100000, − 109 ≤ Ai ≤ 109.

Time limit: 1.0s

Memory limit: 512.0MB
 

(2) Code implementation

        

public class PostfixExpression{

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int add = input.nextInt();
			int reduce = input.nextInt();
			int totalLength = add + reduce + 1;
			int[] number = new int[totalLength];
			for (int i = 0; i < totalLength; i++) {
				number[i] = input.nextInt();
			}
			int sum = 0;
			if (reduce == 0) {
				for (int i = 0; i < totalLength; i++) {
					sum += number[i];
				}
			}
			if (add == 0) {
				Arrays.sort(number);
				if (number[0] < 0) {
					for (int i = 0; i <= reduce; i++) {
						if (number[i] > 0) 
							sum += number[i];
						else
							sum -= number[i];
					}
				} else {
					for (int i = 1; i <= reduce; i++) {
							sum += number[i];
					}
					sum -= number[0];
				}
			}
			if (add != 0 && reduce != 0) {
				int reduceNum = 0;
				for (int i = 0; i < totalLength; i++) {
					if (number[i] < 0) {
						reduceNum++;
					}
				}
				if (reduce >= reduceNum) {
					Arrays.sort(number);
					int temp = reduce;
					for (int i = 0; i < reduceNum; i++) {
						number[i] = -number[i];
						temp--;
					}
					Arrays.sort(number);
					for (int i = totalLength - 1; i >= temp; i--) {
						sum += number[i];
					}
					for (int i = temp - 1; i >= 0; i--) {
						sum -= number[i];
					}
				} else {
					Arrays.sort(number);
					sum += number[totalLength - 1];
					for (int i = 0; i < totalLength - 1; i++) {
						if (number[i] > 0)
							sum += number[i];
						else
							sum -= number[i];
					}
				}
			}
			System.out.println(sum);
		} catch (Exception e) {
			input.close();
		}
	}
}

Topics: Algorithm