HihoCoder 1754数位dp

把c转化成二进制数,然后数位dp即可。

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <math.h>
#include <cstring>
#include <iomanip>
using namespace std;
#define ll long long
#define N 101
#define INF 0x3f3f3f3f

bool ishave[N];
ll binc[N];
ll top = -1;
ll n;
ll dp[N];
ll c;

ll dfs(ll pos,bool limit){
    if(pos == -1){
        return 1;
    }
    if(!limit && dp[pos] != -1){
        return dp[pos];
    }
    ll up = limit?binc[pos]:1;
    ll tmp = 0;
    for(ll i=0;i<=up;i++){
        if(ishave[pos] >= i){
            tmp += dfs(pos-1,limit && (i == binc[pos]));
        }
    }
    if(!limit){
        dp[pos] = tmp;
    }
    return tmp;
}

int main(){
    ll i,j,x,y;
    memset(dp,-1,sizeof(dp));
    scanf("%lld",&n);
    scanf("%lld",&c);
    for(i=1;i<=n;i++){
        scanf("%lld",&x);
        ll cnt = 0;
        while (x > 1)
        {
            cnt++;x/=2;
        }
        ishave[cnt] = 1;
    }
    
    while (c > 0)
    {
        top++;
        binc[top] = c%2;
        c/=2;
    }
    cout<<dfs(top,1)-1<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值