4800: [Ceoi2015]Ice Hockey World Championship

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值