D Knapsack Cryptosystem 2019 cattle off summer school multi-ninth field (binary enumeration)

Link :. D Knapsack Cryptosystem
question is intended : to give you a sequence of length n, and a number s
choice you some numbers from a sequence, and to make their m (to satisfy a unique combination)
and the final output format string 01 (0 means no take, 1 represents taking)
ideas : to start with a question in (wiki knapsack encryption algorithm) .. this question is however nothing to do with
this problem the data range noteworthy: (1 <= n <= 36 ) (0 <= s <9 e18) (ai <2 e17)
if we want to search storms, or the like ordinary pressure DP, a TLE is estimated, a RE
so we can try to enumerate pressure binary form, where we opened map correspond to the map about the state and how much


#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 40; map<ll,ll>G; ll arr[maxn],s; int n; int main(){ cin>>n>>s; { for(int i=0;i<n;i++){ cin>>arr[i]; } int mid = n/2; ll Max = 1<<(n/2); //2e18的状态 for(int i=0;i<Max;i++){ ll sum = 0; for(int j=0;j<mid;j++){ //从右往左数,取第j+1位上的值 if((i>>j)&1) sum += arr[j]; } G[sum] = i; } ll MMax = 1<<((n+1)/2); for(int i=0;i<MMax;i++){ ll sum = 0; for(int j=0;j<((n+1)/2);j++){ if(i>>j&1) sum += arr[mid+j]; } if(G.count(s-sum)){ ll k = G[s-sum]; int cnt = 0;//记录前导零个数 while(k){ //末位为1 cnt++; if(k&1) cout<<1; else cout<<0; k >>= 1; } while(cnt<mid) { cnt++; cout<<0; } cnt = 0; while(i){ cnt ++; if(i&1) cout<<1; else cout<<0; i >>= 1; } while(cnt<(n+1)/2) {cout<<0; cnt++;} return 0; } } } }

Guess you like

Origin www.cnblogs.com/Tianwell/p/11372748.html