BZOJ 4800: [Ceoi2015]Ice Hockey World Championship
Description
有n个物品,m块钱,给定每个物品的价格,求买物品的方案数\(n\leqslant 40,m\leqslant 10^{18}\)。
Solution
meet in middle.
Code
/**************************************************************
Problem: 4800
User: BeiYu
Language: C++
Result: Accepted
Time:5792 ms
Memory:17676 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = 45;
const int N = 1<<21;
int n,md,c;
LL m,ans;
LL a[M],b[N];
void DFS(int x,LL s) {
if(s>m) return;
if(x==md+1) { b[++c]=s;return; }
DFS(x+1,s+a[x]),DFS(x+1,s);
}
void DDFS(int x,LL cur) {
if(cur<0) return;
if(x==n+1) { ans+=upper_bound(b+1,b+c+1,cur)-b-1;return; }
DDFS(x+1,cur-a[x]),DDFS(x+1,cur);
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
md=(n>>1);
DFS(1,0);
sort(b+1,b+c+1);
DDFS(md+1,m);
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号