Blue Bridge Cup past exam questions and buns make up the number

Problem Description

  Xiao Ming eats breakfast at a steamed bun shop almost every morning. He found that this steamed bun shop has N kinds of steamers, and the i-th steamer can hold Ai buns exactly. Each type of steamer has a very large number of cages, which can be considered as infinite cages.


  Whenever a customer wants to buy X steamed buns, the uncle who sells steamed buns will quickly select several cages of steamed buns, so that there are exactly X steamed buns in these cages. For example, there are 3 types of steamers, which can hold 3, 4 and 5 buns respectively. When a customer wants to buy 11 buns, the uncle will choose 2 cages of 3 plus 1 cage of 5 (may also 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 types of steamers, which can hold 4, 5 and 6 buns respectively. And when the customer wanted to buy 7 buns, the uncle couldn't come out.


  Xiao Ming wanted to know how many kinds of numbers there were that Uncle Baozi couldn't figure out.
input format
  The first line contains an integer N. (1 <= N <= 100)
  Each of the following N lines contains an integer Ai. (1 <= Ai <= 100)
output format
  An integer representing the answer. If there are infinite numbers that cannot be made up, output INF.
sample input
2
4
5
Sample output
6
sample input
2
4
6
Sample output
INF
Sample description
  For example 1, the numbers that cannot be made up include: 1, 2, 3, 6, 7, 11.
  For example 2, all odd numbers cannot be made up, so there are infinitely many.

If the greatest common divisor of all numbers is not 1, there are infinite numbers that cannot be made up, that is, INF is output, otherwise it is a 01 backpack.

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <math.h>
 9 #define MAX_N 105
10 #define MAX_M 10050
11 #define ll long long
12 
13 using namespace std;
14 
15 int n;
16 int num[MAX_N];
17 int dp[MAX_M];
18 int yue(int a,int b)
19 {
20     int t;
21     while(b){
22         t = b;
23         b = a % b;
24         a = t;
25     }
26     return a;
27 }
28 int main()
29 {
30     scanf("%d",&n);
31     for(int i = 1; i <= n ;i++)
32         scanf("%d",&num[i]);
33     int ans = num[1];
34     for(int i = 2; i <= n; i++)
35     {
36         ans = yue(ans,num[i]);
37     }
38     if(ans>1)
39     {
40         printf("INF\n");
41     }
42     else
43     {
44         fill(dp,dp+MAX_M,0);
45         int va = 0;
46         dp[0] = 1;
47         for(int i = 1; i <= n; i++)
48         {
49             for(int j = 0; j < MAX_M; j++)
50             {
51                 if(dp[j])
52                 {
53                     dp[j+num[i]] = 1;
54                 }
55             }
56         }
57         for(int i = 1; i < MAX_M; i++)
58             if(dp[i]==0)
59                 va++;
60         printf("%d\n",va);
61     }
62     return 0;
63 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325395192&siteId=291194637