[Algorithm question] Baozi make up the number - JAVA solution

Problem Description:

Xiao Ming eats breakfast at a steamed bun shop almost every morning, and he finds that there are N kinds of steamers in this bun shop, among which there are many kinds of the i-th kind of steamer, which can be considered infinite.

Whenever a customer wants to buy x kinds of steamed buns, the uncle who sells steamed stuffed buns will quickly select a number of steamed buns, so that there are exactly X steamed stuffed buns among these steamed stuffed buns. For example, there are 3 kinds of steamers, which can hold 3, 4, and 5 steamed buns respectively. When a customer wants to buy 11, the uncle will choose 2 cages of 3 plus 1 cage of 5 (or choose 1 cage of 3 plus 2 cages of 4).

Of course, sometimes Uncle Baozi can't make up the quantity that customers want to buy anyway. For example, there are 3 kinds of steamers, which can hold 4, 5 and 6 buns respectively. And when the customer wants to buy 7 buns, the uncle can't play it.
Xiao Ming wants to know how many numbers there are that Uncle Baozi can't make up.


Input format:

The first line contains an integer N. (1 <= N<= 100)
Each of the following N lines contains an integer Ai. (1 <= Aic= 100)
 

Output format:

An integer representing the answer. If there are infinitely many numbers that cannot be made up, output INF.

code:

import java.io.*;
import java.util.Arrays;
public class Main {
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st=new StreamTokenizer(br);
    public static void main(String[] args) throws Exception {
        int n = nextInt();
        int[] arr = new int[101];
        Arrays.fill(arr, 999);
        int t, s = 0;
        for (int i = 0; i < n; i++) {
            t = nextInt();
            if (arr[t] == 999) {
                arr[t] = t;
                s++;
            }
        }
        Arrays.sort(arr);//排序
        if (arr[0]==1){
            System.out.println(0);
            return;
        }
        int k = 10001;
        for (int i=0;i<s-1;i++){
            for (int j=1;j<s;j++){
                if(gcd(arr[i],arr[j])==1){
                    k=Math.min(k,arr[i]*arr[j]);
                }
                if (k==arr[i]*arr[j]||arr[i]*arr[j+1]>=k)
                    break;
            }
            if (arr[i]*arr[i+1]>=k){
                break;
            }
        }
        int []sk=new int[101];
        for (int i=0;i<s;i++){
            for (int j=2;j<=arr[i];j++){
                if (arr[i]%j==0){
                    sk[j]++;
                }
            }
        }
        Arrays.sort(sk);
        if (k>10000&&sk[100]==n){
            System.out.println("INF");
            return;
        }
        int []crr=new int[k+1];
        int []drr=new int[k+1];
        int m=0,co,ss;
        for (int i=0;i<s;i++){
            co=m;
            for (int y=0;y<co;y++){
                for (int j=1;j<=(k-drr[y])/arr[i];j++){
                    ss=drr[y]+arr[i]*j;
                    if (crr[ss]==0){
                        crr[ss]=ss;
                        drr[m]=ss;
                        m++;
                    }
                }
            }
            for (int j=1;j<=k/arr[i];j++){
                if (arr[i]*j>k){
                    break;
                }
                if (crr[arr[i]*j]==0){
                    drr[m]=arr[i]*j;
                    crr[arr[i]*j]=arr[i]*j;
                    m++;
                }
            }
        }
        System.out.println(k-m);
    }
    static  int nextInt() throws Exception{
        st.nextToken();
        return (int)st.nval;
    }
    public static int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }
}


 

Guess you like

Origin blog.csdn.net/SLT1210/article/details/123671522