bzoj4800:[Ceoi2015]Ice Hockey World Championship
题意:有\(n\)个物品,\(m\)块钱,给定每个物品的价格,求买物品的方案数。
\(n<=40,m<=10^{18}\)
水题,\(meet\ in\ the\ middle\)裸题,随便搞搞,统计答案二分一下就没了
#include<cstdio>
#include<algorithm>
int n,sum;long long m,f[1500001],a[41],ans;
void dfs(int x,long long now){
if(x>n/2){f[++sum]=now;return ;}
dfs(x+1,now),dfs(x+1,now+a[x]);}
void dfs1(int x,long long now){
if(x>n){ans+=(std::upper_bound(f+1,f+sum+1,m-now)-f)-1;return ;}
dfs1(x+1,now),dfs1(x+1,now+a[x]);}
int main(){
scanf("%d%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
dfs(1,0);std::sort(f+1,f+sum+1);dfs1(n/2+1,0);
printf("%lld\n",ans);}