Knapsack Cryptosystem(超大01背包——二分+状压)

链接: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

 题意:选若干个物品,使他们的价值之和为s

很显然这是一个体积与价值相等的01背包问题

但是他与普通01背包略有不同

1.并不是体积一定让你求最大价值,而是让求价值等于s的方案

2.价值(体积)太大了,数组开不了那么大。

体积大,价值小还能这样做https://blog.csdn.net/qq_42936517/article/details/96138426

这道题的n很小,然后就想到了状压,状压选择方案,然而n=36,直接状压也不行。

但是前半部分的状态并不会影响后半部分的状态,所以就分成两部分分别状压

状压前半部分的时候,记录一下获得某价值的状态

状压后半部分的时候,判断一下前半部分有没有某种状态的价值与当前状态的价值之和等于s

很明显记录这种事情map很容易就处理了,但是T了(不知道是不是map的原因),用unordered_map过了

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#include<tr1/unordered_map>
using namespace std::tr1;
typedef long long ll;
const ll INF=9e18;
const int N=40;
ll dp1[1<<20];
ll dp2[1<<20];
unordered_map<ll,ll> mp;
ll a[N];
int main(){
    int n;
    ll s;
    scanf("%d%lld",&n,&s);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    int l=n/2;
    int r=n-l;
    int up1=1<<l;
    int up2=1<<r;
    dp1[0]=0;
    mp[0]=0;
    for(int i=0;i<up1;i++){
        for(int j=1;j<=l;j++){
            int k=j-1;
            if(i&(1<<k)) continue;
            int sta=(i|(1<<k));
            dp1[sta]=dp1[i]+a[j];
            mp[dp1[sta]]=sta;
        }
    }
    bool flag=false;
    dp2[0]=0;
    for(int i=0;i<up2;i++){
        for(int j=l+1;j<=n;j++){
            int k=j-l-1;
            if(i&(1<<k)) continue;
            int sta=(i|(1<<k));
            dp2[sta]=dp2[i]+a[j];
            ll p=s-dp2[sta];
            if(mp.find(p)!=mp.end()){
                flag=true;
                int x=mp[p];
                for(int t=0;t<l;t++){
                    int b=x&1;
                    x>>=1;
                    printf("%d",b);
                }
                x=sta;
                for(int t=0;t<r;t++){
                    int b=x&1;
                    x>>=1;
                    printf("%d",b);
                }
                break;
            }
        }
        if(flag) break;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值