Topic link: https: //ac.nowcoder.com/acm/contest/889/D
Meaning of the questions: simple meaning of the questions, from size 36 to a number of elements of the collection and is selected so that their sum.
Ideas: first impression with the search, complexity is 2 ^ 36, need to be optimized, just use binary search. That is half of the previous enumeration, and find whether there is a half ago after record and map enumeration and a half just to meet both for the sum.
AC Code:
#include<cstdio> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; typedef long long LL; int n; LL sum,a[40]; map<LL,string> mp; int main(){ scanf("%d%lld",&n,&sum); for(int i=0;i<n;++i) scanf("%lld",&a[i]); for(int i=0;i<(1<<n/2);++i){ string s; LL tmp=0; for(int j=0;j<n/2;++j) if((i>>j)&1){ tmp+=a[j]; s+='1'; } else{ s+='0'; } mp[tmp]=s; } for(int i=0;i<(1<<(n-n/2));++i){ string s; LL tmp=0; for(int j=0;j<n-n/2;++j) if((i>>j)&1){ tmp+=a[n/2+j]; s+='1'; } else{ s+='0'; } if(mp.count(sum-tmp)){ cout<<mp[sum-tmp]<<s<<"\n"; break; } } return 0; }