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

时间限制: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个数,和一个和s,要在这n个数中找到一个子集使得他们之和等于s并用二进制表示(0代表不用第i个数,1代表用第i个数)

思路:

考虑枚举这n个数,但n最大到36,也就是说最多有2的36次方种情况大约是1e10,这样在1秒内跑不完
于是就需要枚举前n/2个数和后n/2个数,这样最多有2*(2的18次方)种情况,大约是5e5,在1秒内可以跑完
那怎么枚举呢?可以用二进制来表示状态,二进制的第i位为0代表不用第i个数,为1代表用第i个数并加到可能答案中,从0到2的n/2次方也就是从全不选到全选的全排列,
我们把二进制数和他表示的和(可能答案)对应起来,枚举完前n/2个数后在后n/2个数中用s减去当前这种情况得到的可能答案看能否在前n/2种可能答案中找到
如果找得到就说明现在这种情况和前面的那种情况之和能为s,输出二进制即可

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll s,a[],t;
map<ll,ll> mp;
ll convert(ll x,int pos){
ll ans=,i=pos;
while(x){
if(x&)ans+=a[i]; ///如果现在这个状态的这一位为1,则加上这一位的数
x>>=;
i++;
}
return ans;
}
void out(ll x,int cnt){
while(cnt--){
printf("%d",(x&));
x>>=;
}
}
int main(){
scanf("%d%lld",&n,&s);
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
int mid=n/;
for(int i=;i<(<<mid);i++)mp[convert(i,)]=i; ///从0到2的n/2次方也就是从全不选到全选的全排列
for(int i=;i<(<<(n-mid));i++){
t=convert(i,mid+);
if(mp.count(s-t)){
int j=mp[s-t];
out(j,mid); ///要输出n/2个数
out(i,mid); ///要输出n/2个数
}
}
}
/**
给n个数,和一个和s,要在这n个数中找到一个子集使得他们之和等于s并用二进制表示(0代表不用第i个数,1代表用第i个数)
考虑枚举这n个数,但n最大到36,也就是说最多有2的36次方种情况大约是1e10,这样在1秒内跑不完
于是就需要枚举前n/2个数和后n/2个数,这样最多有2*(2的18次方)种情况,大约是5e5,在1秒内可以跑完
那怎么枚举呢?可以用二进制来表示状态,二进制的第i位为0代表不用第i个数,为1代表用第i个数并加到可能答案中,从0到2的n/2次方也就是从全不选到全选的全排列,
我们把二进制数和他表示的和(可能答案)对应起来,枚举完前n/2个数后在后n/2个数中用s减去当前这种情况得到的可能答案看能否在前n/2种可能答案中找到
如果找得到就说明现在这种情况和前面的那种情况之和能为s,输出二进制即可
**/

[状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem的更多相关文章

  1. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  2. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2019牛客暑期多校训练营(第九场)-D Knapsack Cryptosystem (折半搜索)

    题目链接:https://ac.nowcoder.com/acm/contest/889/D 题意:题意简单,从大小为36的集合中选若干元素使得他们的和为sum. 思路:第一感觉用搜索,复杂度为2^3 ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  7. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  8. 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem——哈希表&&二进制枚举

    题意 有长度为 $n$($1\leq n\leq 36$)的数列,给出 $s$,求和为 $s$ 的子集,保证子集存在且唯一. 分析 答案肯定是来自左右半边两部分组成的. 如果我们用哈希表存一半,计算另 ...

  9. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

随机推荐

  1. [Python_scrapy图片爬取下载]

    welcome to myblog Dome地址 爬取某个车站的图片 item.py 中 1.申明item 的fields class PhotoItem(scrapy.Item): # define ...

  2. js的几个库

    http://www.w3.org/TR/FileAPI/ http://www.w3.org/TR/html-media-capture/ demo:http://jsfiddle.net/pmat ...

  3. sycPHPCMS v1.6 cookie sqlinjection

    ./user/index.php include "../include/conn.php"; include "../include/function.php" ...

  4. uploadifive如何动态传参

    直接上代码 关键:$('#file_upload').data('uploadifive').settings.formData = { 'ID': 'ceshi'}; //动态更改formData的 ...

  5. Design Patterns in Android

    对日常在 Android 中实用设计模式进行一下梳理和总结,文中参考了一些网站和大佬的博客,如 MichaelX(xiong_it) .菜鸟教程.四月葡萄.IAM四十二等,在这里注明下~另外强烈推荐图 ...

  6. LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解

    链接 https://leetcode-cn.com/problems/implement-stack-using-queues/ 思路 首先演示push()操作:将元素依次进入队1,进入时用top元 ...

  7. Python——urllib函数网络文件获取

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  8. SpringCloud - 全家桶

    先导篇:SpringCloud介绍篇 第一篇:注册中心Eureka 第二篇:服务提供与Rest+Ribbon调用 第三篇:服务提供与Feign调用 第四篇:熔断器Hystrix(断路器) 第五篇:熔断 ...

  9. Py基础之函数

    '''函数是指一类同类事物的抽象,而且这种抽象可以拓展,并且可以用在同一类事物上'''print (abs(-100),abs(100)) #abs函数是python内置的函数,可以用来求绝对值#pr ...

  10. 趣谈编程史第3期-大器晚成的新晋流量Python发展史

    写在前面 这篇博文主要介绍javaScript的发展史,根据作者在B站发布的同名视频的文案整理修改而成,对视频感兴趣的博友可访问https://www.bilibili.com/video/av860 ...