ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

原 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem 超大背包(二分搜索)

2019-08-16 21:36:57  阅读:251  来源: 互联网

标签:subset val ai ll Cryptosystem 多校 pos 牛客 int


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

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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

题意:给你n个数和一个背包,让你用这n个数恰好放满这个背包。

思路:对于背包问题,经典的解法是DP,复杂度为O(nV),显然不能解决这个问题。而搜索解决01背包的复杂度为O(N*2^N),显然也不能很好解决这个问题。我们可以将这些物品均分为两份,第一份搜出所有情况之后用一个结构体保存起来,第二份搜到边界时二分从第一份种找最符合的。这样时间复杂度为O(N*2^(N/2))。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int maxn=1e6+100;
int n,cnt;
ll s,a[100];
struct node{
	string s;
	ll val;
}ans[maxn];
void dfs1(int pos,ll val,string ss)
{
	if(pos>=n/2)
	{
		ans[++cnt].s=ss;
		ans[cnt].val=val;
		return ;
	}
	dfs1(pos+1,val,ss+"0");
	dfs1(pos+1,val+a[pos],ss+"1");
}
int flag=0;
int judge(ll val)
{
	int l=1,r=cnt,flg=0;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(ans[mid].val+val==s)
		{
			flg=mid;
			break;
		}
		else if(ans[mid].val+val<s) l=mid+1;
		else r=mid-1;
	}
	return flg;
}
void dfs2(int pos,ll val,string ss)
{
	if(pos>n)
	{
		int pos=judge(val);
		if(pos!=0)
		{
			flag=1;
			cout<<ans[pos].s<<ss<<endl;
		}
		return ;
	}
	if(flag) return ;
	dfs2(pos+1,val,ss+"0");
	dfs2(pos+1,val+a[pos],ss+"1");
}
bool cmp(node x,node y)
{
	return x.val<y.val;
}
int main()
{
	string ss;
	scanf("%d%lld",&n,&s);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	ss="";cnt=0;flag=0;
	dfs1(1,0,ss);
	ss="";
	sort(ans+1,ans+1+cnt,cmp);
	dfs2(n/2,0,ss);
	return 0;
}

 

标签:subset,val,ai,ll,Cryptosystem,多校,pos,牛客,int
来源: https://blog.csdn.net/why932346109/article/details/99685662

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有