Knapsack in a Globalized World
时间限制: 1 Sec 内存限制: 64 MB
提交: 118 解决: 38
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Globalization stops at nothing, not even at the good old honest profession of a burglar. Nowadays it is not enough to break in somewhere, take everything you can carry and dart off. No! You have to be competitive, optimize your profit and utilize synergies.
So, the new game rules are:
• break only into huge stores, so there is practically endless supply of any kind of items;
• your knapsack should be huge;
• your knapsack should be full (there should be no empty space left).
Damn you, globalization, these rules are not easy to follow! Luckily, you can write a program, which will help you decide whether you should loot a store or not.
输入
The input consists of:
• one line with two integers n (1 ≤ n ≤ 20) and k (1 ≤ k ≤ 1018 ), where n is the number of different item types and k is the size of your knapsack;
• one line with n integers g 1 , . . . , gn (1 ≤ gi ≤ 103 for all 1 ≤ i ≤ n), where g1 , . . . , gn are the sizes of the n item types.
输出
Output “possible” if it is possible to fill your knapsack with items from the store (you may assume that there are enough items of any type), otherwise output “impossible”.
样例输入
2 10000000000 3 6
样例输出
impossible
来源/分类
解题思路:给了一个容量很大的背包,然后给了你一些物品,问你能够装满。
感觉做的时候思维被禁锢了,一直在想背包容量这么大怎么去背啊。。。。
看了题解,发现大家都是去找一个当前背包的因子。。。如果这个因子能够装满的话,背包就能够装满。
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define pb(x) push_back(x)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define LL long long
#define N 300
#define MAXN 2000050
#define inf 0x3f3f3f3f
LL dp[MAXN+5];
LL a[25];
int main()
{
int n;
LL w;
scanf("%d%lld",&n,&w);
rep(i,1,n)scanf("%lld",&a[i]);
memset(dp,0,sizeof(dp));
dp[0]=1;
rep(i,1,n)
{
for(int j=0;j<=MAXN;j++)
{
if(dp[j]&&j+a[i]<=MAXN)
{
dp[j+a[i]]=1;
}
}
}
int f=0;
rep(i,1,MAXN)
{
if(dp[i] && w%i==0)
{
f=1;
break;
}
}
if(f)puts("possible");
else puts("impossible");
}