1. 程式人生 > >[Ceoi2015]Ice Hockey World Championship

[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);}