http://lx.lanqiao.cn/problem.page?gpid=T440
问题描述
小明几乎每天早晨都会在一家包子铺吃早餐。他发现这家包子铺有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。
样例输入
2
4
5
样例输出
6
样例输入
2
4
6
样例输出
INF
样例说明
对于样例1,凑不出的数目包括:1, 2, 3, 6, 7, 11。
对于样例2,所有奇数都凑不出来,所以有无限多个。
数据规模和约定
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。
提交程序时,注意选择所期望的语言类型和编译器类型。
这是正解 下面还有个骗分的
refer to
https://blog.csdn.net/qq_34594236/article/details/70803797
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
int N, M;
using namespace std;
const int si =10007;
bool dp[10007];
int arr[107];
int gcd(int a, int b) {
if (b) return gcd(b, a % b);
return a;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
int g = arr[0];
for (int i = 1; i < N; i++)
g = gcd(g, arr[i]);
if (g != 1) {
printf("INF\n");
return 0;
}
dp[0] = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < si - arr[i]; j++) {
if (dp[j])
dp[j + arr[i]] = 1;
}
}
int ans = 0;
for (int i = 1; i < si; i++)
if (!dp[i]) ans++;
cout << ans << endl;
return 0;
}
骗分 直接当成多重背包做 设定一个阈值 要是不能搞出来的值多余这个阈值 就认为是INF 否则认为是可行的 测试表明 当阈值为1000时得到75分 3000时87分 5000时得到全分
si = 2e5
总的计算量是2e7 这道题的循环体很简单可以大胆些设置成4e7 然后设定阈值可以设大些 更精准
测试表明 si = 4e5, 总的计算量是4e7 阈值=5000时也是100分 跑了16ms
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
int N, M;
using namespace std;
const int si =20007;
bool dp[si];
int arr[107];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
dp[0] = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < si - arr[i]; j++) {
if (dp[j])
dp[j + arr[i]] = 1;
}
}
int ans = 0;
for (int i = 1; i < si; i++)
if (!dp[i])
ans++;
if (ans >= 5000)
printf("INF\n");
else
cout << ans << endl;
return 0;
}
网友评论