链接: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. Ubuntu18.04制作本地源

    Ubuntu 18.04 制作本地源 1. 在可联网的Ubuntu18.04上制作源 创建目录 mkdir /opt/debs 最好在目标电脑上创建相同的目录,以免 apt-get install 时 ...

  2. Linux IO多路复用

    监听文件描述符的状态来进行相应的读写操作,3个函数: 123 selectpollepoll 123456789 int (int nfds, fd_set *readfds, fd_set *wri ...

  3. CPU网卡亲和绑定

    #!/bin/bash # # Copyright (c) , Intel Corporation # # Redistribution and use in source and binary fo ...

  4. Kafka配置文件及解释

    broker.id=0num.network.threads=9num.io.threads=24socket.send.buffer.bytes=102400listeners=PLAINTEXT: ...

  5. Centos 7 使用Securecrt 配置Public key 登录

    环境:Centos 7 SecureCRT 版本:8.0.4 需求:配置使用Public key 登录服务器禁用密码登录 1. 配置使用SecureCRT,生成Public key 跟私钥 2. 配置 ...

  6. 爬虫(三)解析js,抓取优酷免费视频的真实播放地址

    工具:google浏览器 + fiddler抓包工具 说明:这里不贴代码,[只讲思路!!!] 原始url = https://v.youku.com/v_show/id_XMzIwNjgyMDgwOA ...

  7. 密码学习(一)——Base64

    简介 Base64是一种非常常用的数据编码方式,标准Base64可以把所有的数据用"A~Z,a~z,0~9,+,/,="共65个字符(‘=’号仅是一个占位符,作为后缀)表示,当然在 ...

  8. JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计

    5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...

  9. vue开发路由相关基础知识和笔记

    路由实现:hash模式 和 history模式 hash模式: 概述 在浏览器中符号"#",#以及#后面的字符称之为hash,用window.location.hash读取: 特点 ...

  10. xcode制作越狱包

    1.将运行目标选为iOS Device 2.Edit Scheme -> 选择 Run [App Name] -> Build Configuration下拉框中选择Release 3.生 ...