@ 2016GCPC Knapsack in a Globalized World (DP 完全背包)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82954277

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 mayassume that there are enough items of any type), otherwise output “impossible”.

样例输入

2 10000000000
3 6

样例输出

impossible

[题意]

给 n 个 物品 和 背包容量 (  容量很大)   物品数量无限

问 能不能  用这 n 个 物品 (其中一部分 或 全部)  恰好填满背包.,

[显然 一个 完全背包问题]

但是  背包的容量 很大,  ll  级别.   dp  不出来.

但是  我们可以去 dp  背包的因子啊.   如果他的因子 能被 背出来., 那么他一定可以.  倍数关系...

认为因子在 2e6的 范围内 都能找到  k 的因子.

所以 只需要 dp 到 2e6 就可以.

[代码]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)

typedef long long ll;
const int maxn = 2e6+10;
const int mod =1e9+7;
const int inf = 0x3f3f3f3f;
using namespace std;

int c[100];
int dp[maxn];
int main(int argc, char const *argv[])
{
	ll n,k;
	scanf("%lld %lld",&n,&k);
	rep(i,1,n) scanf("%d",&c[i]);
	memset(dp,0,sizeof(dp));
	dp[0] = 1;
	for(int i = 1; i <= n; i++)
	{
    	for(int j = 0  ; j <= maxn-1 ; j ++ )
    	{
        	if( dp[j] && j + c[i] <= maxn-1 )
        		dp[j+c[i]] = 1;
		}
	}
	int flag = 0;
	rep(i,1,maxn-1)
	{
		if( dp[i] && k%i==0)
		{
			flag = 1;
			break;
		}
	}
	if( flag) printf("possible\n");
	else printf("impossible\n");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82954277