某种密码(折半暴搜)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=505;
ll n,key;
ll a[maxn];
map < ll , ll > t;
ll ans;
void dfs1(ll now,ll sum)
{
    if(now==n/2+1)
    {
        t[sum]++;
        return;
    }
    dfs1(now+1,sum+a[now]);
    dfs1(now+1,sum);
}
void dfs2(ll now,ll sum)
{
    if(now==n+1)
    {
        ans+=t[key-sum];
        return;
    }
    dfs2(now+1,sum+a[now]);
    dfs2(now+1,sum);
}
int main()
{
    scanf("%lld%lld",&n,&key);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    dfs1(1,0);
    dfs2(n/2+1,0);
    printf("%lld",ans);
    return 0;
}

 

posted @ 2019-10-29 19:40  阿基米德的澡盆  阅读(120)  评论(0编辑  收藏  举报