包子凑数

题意

给 $n(1\le n\le 100)$ 个数$(1\le a_i\le 100)$,问由它们不能组合(加法)成的数(正整数)的个数,若有无穷个输出”INF”。

题解

若它们的公共 gcd 不为1,输出 INF,构造一个小顶堆,每次新加的值为堆顶的值加$a_i$ ,弹出堆顶,可证最大不可表示的值不会超过1e4,所以当堆顶大于1e4的时候跳出循环。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
void in(int &a,int &b,int &c,int &d){scanf("%d%d%d%d",&a,&b,&c,&d);}
void in(int &a,int &b,int &c){scanf("%d%d%d",&a,&b,&c);}
void in(int &a,int &b){scanf("%d%d",&a,&b);}
void in(ll &a){scanf("%lld",&a);}
void in(int &a){scanf("%d",&a);}
void in(char *s){scanf("%s",s);}
void in(db &a){scanf("%Lf",&a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 1.1e5;
ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps; }
int db_cmp(db a, db b){ return sign(a-b); }

int gcd(int a,int b){
return b?gcd(b,a%b):a;
}

priority_queue<int,vii,greater<int> >que;
bool vis[11000];
int main() {
#ifdef PerpEternal
// freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin);
// freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);
#endif
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,a[110];
mem0(vis);
in(n);
fro0(i,n)in(a[i]);
if(n==1){
if(a[0]==1)puts("0");
else puts("INF");
}else{
int gc=a[0];
for1(i,n-1)gc=gcd(gc,a[i]);
if(gc==1){
int ans=0;
sort(a,a+n);
fro0(i,n){
vis[a[i]]=1;
que.push(a[i]);
}
while(1){
int to=que.top();
que.pop();
if(to>=1e4)break;
fro0(i,n){
int now=to+a[i];
if(now<=1e4&&vis[now]==0){
vis[now]=1;
que.push(now);
}
}
}
for1(i,1e4)if(!vis[i])ans++;
printf("%d\n",ans);
}else puts("INF");
}
return 0;
}