包子凑数
问题描述
小明几乎每天早晨都会在一家包子铺吃早餐。他发现这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子。每种蒸笼都有非常多笼,可以认为是无限笼。
每当有顾客想买X个包子,卖包子的大叔就会迅速选出若干笼包子来,使得这若干笼中恰好一共有X个包子。比如一共有3种蒸笼,分别能放3、4和5个包子。当顾客想买11个包子时,大叔就会选2笼3个的再加1笼5个的(也可能选出1笼3个的再加2笼4个的)。
当然有时包子大叔无论如何也凑不出顾客想买的数量。比如一共有3种蒸笼,分别能放4、5和6个包子。而顾客想买7个包子时,大叔就凑不出来了。
小明想知道一共有多少种数目是包子大叔凑不出来的。
输入格式
第一行包含一个整数N。(1 <= N <= 100)
以下N行每行包含一个整数Ai。(1 <= Ai <= 100)
输出格式
一个整数代表答案。如果凑不出的数目有无限多个,输出INF。
思路正解:若n个数不互质(即这n个数的最大公约数大于1),则肯定为INF,否则必定有解,统计结果就好.
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map::iterator it;
int n,dp[maxn],w[maxn];
int gcd(int x,int y)
{
return x%y == 0?y:gcd(y,x%y);
}
int main()
{
cin>>n;
cin>>w[1];
int tmp = w[1];
for(int i = 2;i<= n;i++)
scanf("%d",&w[i]),tmp = gcd(tmp,w[i]);
dp[0] = 1;
for(int i = 1;i<= n;i++)
{
for(int j = w[i];j< maxn;j++)
dp[j] = max(dp[j],dp[j-w[i]]);
}
int ans = 0;
for(int i = 1;i< maxn;i++)
if(!dp[i])
ans++;
if(tmp> 1)
cout<
else
cout<
return 0;
}