zoukankan      html  css  js  c++  java
  • [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接: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,输出二进制即可

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 int n;
     5 ll s,a[40],t;
     6 map<ll,ll> mp;
     7 ll convert(ll x,int pos){
     8     ll ans=0,i=pos;
     9     while(x){
    10         if(x&1)ans+=a[i];       ///如果现在这个状态的这一位为1,则加上这一位的数
    11         x>>=1;
    12         i++;
    13     }
    14     return ans;
    15 }
    16 void out(ll x,int cnt){
    17     while(cnt--){
    18         printf("%d",(x&1));
    19         x>>=1;
    20     }
    21 }
    22 int main(){
    23     scanf("%d%lld",&n,&s);
    24     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    25     int mid=n/2;
    26     for(int i=0;i<(1<<mid);i++)mp[convert(i,1)]=i;  ///从0到2的n/2次方也就是从全不选到全选的全排列
    27     for(int i=0;i<(1<<(n-mid));i++){
    28         t=convert(i,mid+1);
    29         if(mp.count(s-t)){
    30             int j=mp[s-t];
    31             out(j,mid);         ///要输出n/2个数
    32             out(i,mid);         ///要输出n/2个数
    33         }
    34     }
    35 }
    36 /**
    37 给n个数,和一个和s,要在这n个数中找到一个子集使得他们之和等于s并用二进制表示(0代表不用第i个数,1代表用第i个数)
    38 考虑枚举这n个数,但n最大到36,也就是说最多有2的36次方种情况大约是1e10,这样在1秒内跑不完
    39 于是就需要枚举前n/2个数和后n/2个数,这样最多有2*(2的18次方)种情况,大约是5e5,在1秒内可以跑完
    40 那怎么枚举呢?可以用二进制来表示状态,二进制的第i位为0代表不用第i个数,为1代表用第i个数并加到可能答案中,从0到2的n/2次方也就是从全不选到全选的全排列,
    41 我们把二进制数和他表示的和(可能答案)对应起来,枚举完前n/2个数后在后n/2个数中用s减去当前这种情况得到的可能答案看能否在前n/2种可能答案中找到
    42 如果找得到就说明现在这种情况和前面的那种情况之和能为s,输出二进制即可
    43 **/
  • 相关阅读:
    [C#] 主窗口嵌入子窗口(绑架窗口)
    【WP7】后台加载数据BackgroundWorker
    【WP7】手势操作与拨号键盘
    【算法】拼音匹配算法(支持多音字)
    【笔记】歌词显示问题
    【笔记】使用千千静听服务器下载歌词
    【WP7】代码创建png图片
    【WP7】自定义字体
    【WP7】对象序列化
    【WP7】控件倾斜特效
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11381760.html
Copyright © 2011-2022 走看看