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

在全球化的影响下,传统的盗窃行为已经发生了变化。新的游戏规则要求窃贼必须针对大型商店行动,利用无限的商品供应,携带巨大的背包,并确保背包完全装满。本文探讨了如何通过编程来决定是否应该掠夺一家商店,提出了一种解决大背包容量问题的方法,即通过寻找背包容量的因子来判断是否能够使用给定的物品类型恰好填满背包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值