第八届蓝桥杯省赛C/C++ A组第8题 包子凑数

时间:2022-09-10 11:46:27

参考了http://blog.csdn.net/y1196645376/article/details/69718192

思路:

数论+完全背包。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 const int MAXN = 100005;
 6 
 7 int a[105], ok[MAXN], n;
 8 
 9 int gcd(int x, int y)
10 {
11     return !y ? x : gcd(y, x % y);
12 }
13 
14 int main()
15 {
16     cin >> n;
17     for (int i = 0; i < n; i++)
18     {
19         cin >> a[i];
20     }
21     int g = a[0];
22     for (int i = 1; i < n; i++)
23     {
24         g = gcd(g, a[i]);
25     }
26     if (g != 1)
27         puts("INF");
28     else
29     {
30         ok[0] = true;
31         for (int i = 0; i < n; i++)
32         {
33             for (int j = 0; j + a[i] < MAXN; j++)
34             {
35                 if (ok[j])
36                 {
37                     ok[j + a[i]] = true;
38                 }
39             }
40         }
41         int cnt = 0;
42         for (int i = 0; i < MAXN; i++)
43         {
44             if (!ok[i])
45                 cnt++;
46         }
47         printf("%d\n", cnt);
48     }
49     return 0;
50 }