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

本文介绍了一种使用折半搜索解决特定背包问题的方法,针对Merkle-Hellman背包密码系统,通过预处理前半部分元素的所有可能组合,再用后半部分元素进行匹配,确保找到唯一解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

题目大意:

给你一个背包,容量是 9e18
然后有n个物品 n<=36 每个物品都有个体积

问选哪些物品可以将这个背包装满。(题目保证一定有答案而且唯一)
输出一个字符串 选就是1 不选就是0

解题思路:

一开始搜索+剪枝 T
正解应该是
把前 n/2 个物品的所有情况都算出来,然后存在一个ans 结构体里面,需要存的是这个结果,还有这个结果对应的 “字符串” 这个字符串就是表示 某个位置选不选的字符串。

然后再用dfs跑剩下的 n/2 个数,对于每个和 都去ans里面 二分找 看看能不能找到一个和当前结果 和 是背包容量的值。找到了就是结果了,因为保证答案唯一。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e6;
int n;
ull sum;
struct node{
    ull sum_ans;
    string s;
}ans[maxn];
ull a[40];
int cnt = 0;
bool cmp(node x,node y){
    return x.sum_ans<y.sum_ans;
}
int find(ull now){
    int left = 0;
    int right = cnt-1;
    int res = -1;
    while(left<=right){
        int mid = (left+right)/2;
        if(ans[mid].sum_ans+now == sum){
            res = mid;
            break;
        }
        if(ans[mid].sum_ans+now < sum){
            left = mid+1;
        }else {
            right = mid-1;
        }
    }
    return res;
}
void dfs1(int x,ull now,string ANS){
    //选
    if(x>=n/2){
        ans[cnt].sum_ans = now;
        ans[cnt].s = ANS;
        cnt++;
        return;
    }
    dfs1(x+1,now+a[x],ANS+"1");
    dfs1(x+1,now,ANS+"0");
}
int flag = 0;
void dfs2(int x,ull now,string ANS){
    if(x>n){
        int id = -1;
        id = find(now);
        if(id != -1){
            cout<<ans[id].s+ANS<<endl;
            flag = 1;
        }
        return ;
    }
    if(flag) return;
    dfs2(x+1,now+a[x],ANS+"1");
    dfs2(x+1,now,ANS+"0");
}
int main(){
    scanf("%d%lld",&n,&sum);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    string ss="";
    dfs1(1,0,ss);
    sort(ans,ans+cnt,cmp);
    string sss="";
    dfs2(n/2,0,sss);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值