2019暑假牛客多校第九场D.Knapsack Cryptosystem 折半搜索(dfs或状压)

D.Knapsack Cryptosystem

传送门
题目描述
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 ∗ 1 0 18 9 *10^{18} 91018)
The second line contains n integers, which are {ai}(0 < ai < 2 * 1 0 17 10^{17} 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(<=36)个数,让你找出其中和等于k的组成。
题目思路:
由于ai太大,和n有36个不可以爆搜,也不可以背包。
所以这里要折半爆搜。就是分成两部分。
首先求得一部分的和,排序。最多有 2 18 2^{18} 218(262144)
其次再遍历另一半的和(最多有 2 18 2^{18} 218(262144)),用二分(复杂度log262144==18)找前面一半是否有和它匹配的,若有,则输出,没有继续。
总复杂度为O(262144+262144*18)约等于4000000.
用状压模拟。(i<<k)0,1表示状态。

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int n;
ll s;
ll num[40];
struct node{
    ll x;  //存前一半的值
    int id;  //存状压的状态01
    node(ll _x = 0, int _id = 0) : x(_x), id(_id) {}  //用于结构体lower_bound查找
    bool operator<(const node &b) { return x < b.x; }
}dis[1500005];

bool cmp(const node&a,const node&b){
    return a.x<b.x;
}

int main(){
    cin>>n>>s;
    for(int i=1;i<=n;i++){
        cin>>num[i];
    }
    int k=n/2;
    int many1=(1<<(k));   //前一半和的数量
    for(int i=0;i<=many1+5;i++) dis[i].x=0;
    for(int i=0;i<many1;i++){  //爆搜
        int c=i,cnt=1;
        while(c){
            if(c&1) dis[i].x+=num[cnt];  //1代表取
            cnt++;
            c>>=1;  //判断下一位
        }
        dis[i].id=i;
    }

    sort(dis,dis+many1,cmp);  //给前一半排序
    int k2=n-k;
    int many2=(1<<(k2));  //后一半的数量
    for(int i=0;i<many2;i++){  //爆搜
        int c=i,cnt=k+1; //从第k+1个开始
        ll ans=0;
        while(c){
            if(c&1) ans+=num[cnt];   //1为取
            cnt++;
            c>>=1;
        }
        ll find;
        find=s-ans;  //需要在前一个和里找的值
        int x=lower_bound(dis,dis+many1,node(find,0))-dis; 
        if(ans+dis[x].x==s){  //判断和是否为s
            int cnt2=0;  //数前一半的个数
            while(dis[x].id){
                cnt2++;
                if(dis[x].id&1) printf("1");
                else printf("0");
                dis[x].id>>=1;
            }
            if(cnt2<k){  //若还差,应补上0
                for(int j=cnt2+1;j<=k;j++) printf("0");
            }
            
            cnt2=0;//数后一半的个数
            int c=i;
             while(c){
                cnt2++;
                if(c&1) printf("1");
                else printf("0");
                c>>=1;
            }
            if(cnt2<k2){  //若还差,应补上0
                for(int j=cnt2+1;j<=k2;j++) printf("0");
            }
            return 0;
        }
    }
}

更多多校信息←请点击这里

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值