Knapsack Cryptosystem(2019牛客多校折半查询)

链接:https://ac.nowcoder.com/acm/contest/889/D
来源:牛客网

Amy asks Mr. B problem D. Please help Mr. B to solve the following problem.

Amy wants to crack Merkle–Hellman knapsack cryptosystem. Please help it.

Given an array {ai} with length n, and the sum s.
Please find a subset of {ai}, such that the sum of the subset is s.

For more details about Merkle–Hellman knapsack cryptosystem Please read
https://en.wikipedia.org/wiki/Merkle%E2%80%93Hellman_knapsack_cryptosystem
https://blog.nowcoder.net/n/66ec16042de7421ea87619a72683f807
Because of some reason, you might not be able to open Wikipedia.
Whether you read it or not, this problem is solvable.
输入描述:
The first line contains two integers, which are n(1 <= n <= 36) and s(0 <= s < 9 * 1018)
The second line contains n integers, which are {ai}(0 < ai < 2 * 1017).

{ai} is generated like in the Merkle–Hellman knapsack cryptosystem, so there exists a solution and the solution is unique.
Also, according to the algorithm, for any subset sum s, if there exists a solution, then the solution is unique.
输出描述:
Output a 01 sequence.

If the i-th digit is 1, then ai is in the subset.
If the i-th digit is 0, then ai is not in the subset.
示例1
输入
复制
8 1129
295 592 301 14 28 353 120 236
输出
复制
01100001
说明
This is the example in Wikipedia.
示例2
输入
复制
36 68719476735
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648 4294967296 8589934592 17179869184 34359738368
输出
复制
111111111111111111111111111111111111
卡了好久这个题,做出来之后才想起来原来之前学校校赛的时候出过类似的题目,泪目!!
要是直接做的话,2^36肯定会超时,那么我们变换一下,求一下这个序列的前一半的所有组合的值,总共 2^18中情况,并且把路径记录下来。剩下的一半找出来之后,二分查找能否组成m。然后输出路径就行。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e6+100;
struct node{
	ll sum;
	string s;
	bool operator <(const node &a)const
	{
		return a.sum>sum;
	}
}p[maxx];
ll a[maxx],b[maxx];
int n,cnt,flag=0;ll m;

void dfs(int pos,int top,ll sum,string s)
{
	if(pos==top)
	{
		p[++cnt].sum=sum;
		p[cnt].s=s;
		return ;
	}
	dfs(pos+1,top,sum,s+"0");
	dfs(pos+1,top,sum+a[pos],s+"1");
}
void dfs1(int pos,int top,ll sum,string s)
{
	if(flag) return ;
	if(pos==top)
	{
		//if(s=="111111111111111111") cout<<sum<<endl;
		int x=lower_bound(b+1,b+1+cnt,m-sum)-b;
		if(b[x]+sum==m) 
		{
			cout<<p[x].s<<s<<endl;
			flag=1;
		}
		return;
	}
	dfs1(pos+1,top,sum,s+"0");
	dfs1(pos+1,top,sum+a[pos],s+"1");
}
int main()
{
	while(~scanf("%d%lld",&n,&m))
	{
		cnt=0;flag=0;
		for(int i=0;i<n;i++) scanf("%lld",&a[i]);
		dfs(0,n/2,0,"");
		sort(p+1,p+1+cnt);
		for(int i=1;i<=cnt;i++) b[i]=p[i].sum;
		dfs1(n/2,n,0,"");
	}
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值