4800: [Ceoi2015]Ice Hockey World Championship

来源:互联网 发布:做淘宝客服的好处 编辑:程序博客网 时间:2024/05/17 03:27

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

0 0
原创粉丝点击