大奖赛

9 篇文章 0 订阅
4 篇文章 0 订阅

30暴力搜
60 01背包
100 折半搜索

以上三个做法都很裸

顺便Lancelot+Morgan
长江骑士和王姐
怕不是某呆毛王要提着咖喱棒来打架

60分背包
忘了+0
结果一直少1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<climits>
#include<queue>
#define LL long long
using namespace std;

int n,k,i,j,pr[45];
LL dp[10005];

int main()
{
    freopen("champion.in","r",stdin);
    freopen("champion.out","w",stdout); 

    scanf("%d%d",&n,&k);

    for(i=1;i<=n;i++)
        scanf("%d",&pr[i]);

    dp[0]=1;    

    for(i=1;i<=n;i++)
        for(j=k;j>=pr[i];j--)
            dp[j]+=dp[j-pr[i]];

    for(i=0;i<k;i++) dp[k]+=dp[i];  


    printf("%lld",dp[k]);
    return 0;
}

100分正解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<climits>
#include<cstdlib>
#include<ctime>
#include<cstring>
#include<queue>
#define LL long long
using namespace std;

LL tot1,sum1[1100000],n1,n2,n,k,ans,a[45];

void dfs1(LL x,LL tot)
{
    if(x==n1+1)
    {
        tot1++;
        sum1[tot1]=tot;
        return;
    }

    if(tot+a[x]<=k) dfs1(x+1,tot+a[x]);
    dfs1(x+1,tot);
}

void dfs2(LL x,LL tot)
{
    if(x==n+1)
    {
        LL p,pos;

        p=k-tot;
        pos=upper_bound(sum1+1,sum1+tot1+1,p)-sum1;
        ans+=pos-1;
        return;
    }

    if(tot+a[x]<=k) dfs2(x+1,tot+a[x]);
    dfs2(x+1,tot);
}

void predfs1()
{
    n1=n/2;
    dfs1(1,0);
    sort(sum1+1,sum1+tot1+1);
}

void predfs2()
{
    n2=n-n1;
    dfs2(n1+1,0);
}

int main()
{
    freopen("champion.in","r",stdin);
    freopen("champion.out","w",stdout); 

    LL i;

    scanf("%lld%lld",&n,&k);

    for(i=1;i<=n;i++)
        scanf("%lld",&a[i]);

    predfs1();
    predfs2();

    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值