鏈接: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
題目大意:
先輸入一個數字nnn,代表有一個大小為nnn的數組(1≤n≤36)(1\le n \le 36)(1≤n≤36),再輸入一個數字sss,下面一行輸入nnn個數字,要從這nnn個數字里面選取任意個數字使其和等于sss,通過輸出一串長度為nnn的01字符串來表示選或不選。
解題思路:
首先想到的是枚舉所有選擇的情況,但發現其時間復雜度已經到達了2362^{36}236,明顯會超時,因此想到折半的方法,將其時間復雜度降為2182^{18}218,接近1e61e61e6,也就是將數組分為兩部分,枚舉前半部份所有的選擇情況,將其和存在一個數組中,同理處理后半部分,后面判斷其和是否能組成sss,僅需枚舉前半部分的所有情況,通過用 s?s-s?前半部分的和 ,查找其在后半部分是否存在即可。
代碼:
//#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
ll arr[40];
struct node1
{ll num;int id;
}sum_qian[1001000];
struct node2
{ll num;int id;
}sum_hou[1001000];
bool cmp1(node1 a,node1 b) {return a.num<b.num;
}
bool cmp2(node2 a,node2 b) {return a.num<b.num;
}
ll get_sum(int id,int len) {ll sum=0;while(id) {if(id&1) {sum+=arr[len];}id>>=1;len++;}return sum;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);int n;ll s;cin>>n>>s;rep(i,1,n) {cin>>arr[i];}int nape=n/2;for(int i=0;i<(1<<nape);i++) {sum_qian[i].num=get_sum(i,1);sum_qian[i].id=i;}int len_qian=(1<<nape);int t1=nape;int start=nape+1;nape=n-nape;for(int i=0;i<(1<<nape);i++) {sum_hou[i].num=get_sum(i,start);sum_hou[i].id=i;}int len_hou=(1<<nape);int t2=nape;sort(sum_qian,sum_qian+len_qian,cmp1);sort(sum_hou,sum_hou+len_hou,cmp2);int qian=-1,hou=-1;for(int i=0;i<len_qian;i++) {ll nape;if(s>=sum_qian[i].num)nape=s-sum_qian[i].num;else continue;int l=0,r=len_hou;int id=-1;while(r-l>1) {int mid=(r+l)>>1;if(sum_hou[mid].num==nape) {id=sum_hou[mid].id;break;}else if(sum_hou[mid].num>nape) {r=mid;}else {l=mid;}}if(id!=-1) {qian=sum_qian[i].id;hou=id;break;}}if(qian!=-1&&hou!=-1) {for(int i=1;i<=t1;i++) {cout<<(qian&1);qian>>=1;}for(int i=1;i<=t2;i++) {cout<<(hou&1);hou>>=1;}cout<<endl;}return 0;
}