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