您的位置:首页 > 其它

4800: [Ceoi2015]Ice Hockey World Championship

2017-04-10 18:42 302 查看

4800: [Ceoi2015]Ice Hockey World Championship

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 257  Solved: 135

[Submit][Status][Discuss]

Description

有n个物品,m块钱,给定每个物品的价格,求买物品的方案数。

Input

第一行两个数n,m代表物品数量及钱数
第二行n个数,代表每个物品的价格
n<=40,m<=10^18

Output

一行一个数表示购买的方案数
(想怎么买就怎么买,当然不买也算一种)

Sample Input

5 1000

100 1500 500 500 1000

Sample Output

8

HINT

Source



[Submit][Status][Discuss]


采取meet-in-middle的思想,每20个数字分一组,搜出所有组合,然后统计前缀和就行了#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = (1 << 21);
typedef long long LL;

int n,tp;
LL m,Ans,A[55],B[55],a[2][maxn];

void Dfs(int x,LL sum,int now)
{
if (x == tp + 1)
{
a[now][++a[now][0]] = sum;
return;
}
Dfs(x + 1,sum,now);
if (B[x] <= m - sum) Dfs(x + 1,sum + B[x],now);
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

cin >> n >> m;
for (int i = 1; i <= n; i++) scanf("%lld",&A[i]);
if (n == 1)
{
cout << (A[1] <= m ? 2 : 1) << endl;
return 0;
}
int N = n / 2; for (int i = 1; i <= N; i++) B[++tp] = A[i]; Dfs(1,0,0);
tp = 0; for (int i = N + 1; i <= n; i++) B[++tp] = A[i]; Dfs(1,0,1);
sort(a[0] + 1,a[0] + a[0][0] + 1);
sort(a[1] + 1,a[1] + a[1][0] + 1); int pos = a[1][0];
for (int i = 1; i <= a[0][0]; i++)
{
while (pos && a[1][pos] > m - a[0][i]) --pos;
Ans += 1LL * pos;
}
cout << Ans << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: