AcWing 1226. 包子凑数
原题链接
中等
作者:
辰风
,
2020-02-24 10:33:39
,
所有人可见
,
阅读 2970
分析
首先结论是如果所有的数的最大公约数不为1,就有不能凑出的数,并且小于10000,否则就有无限个
二维做法
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10010;
int a[110];
bool f[110][N];
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int n;
scanf("%d", &n);
int d = 0;
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &a[i]);
d = gcd(d, a[i]);
}
if (d != 1) puts("INF");
else
{
f[0][0] = true;
for (int i = 1; i <= n; i ++ )
for (int j = 0; j < N; j ++ )
{
f[i][j] = f[i - 1][j];
if (j >= a[i]) f[i][j] |= f[i][j - a[i]];
}
int res = 0;
for (int i = 0; i < N; i ++ )
if (!f[n][i])
res ++ ;
printf("%d\n", res);
}
return 0;
}
一维做法
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10010;
int a[110];
bool f[N];
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int n;
scanf("%d", &n);
int d = 0;
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &a[i]);
d = gcd(d, a[i]);
}
if (d != 1) puts("INF");
else
{
f[0] = true;
for (int i = 1; i <= n; i ++ )
for (int j = a[i]; j < N; j ++ )
f[j] |= f[j - a[i]];
int res = 0;
for (int i = 0; i < N; i ++ )
if (!f[i])
res ++ ;
printf("%d\n", res);
}
return 0;
}
大佬 问一下|是代表f[i][j]和f[i][j-w]任一为真 就为真吗