标签(空格分隔): 数位DP
DP 显然每一位只有选一或选0的情况
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int X,Y,K,B,dit[35],dp[35][25];
int dfs(int step,int c,int lim)
{
if(step==0)return c==K;
if(lim==0&&dp[step][c]!=-1)return dp[step][c];
int res=0;
if(c<K)if(lim==0||dit[step])res+=dfs(step-1,c+1,lim&&dit[step]==1);
res+=dfs(step-1,c,lim&&dit[step]==0);
if(lim==0)dp[step][c]=res;
return res;
}
int calc(int x)
{
int len=0;
while(x)dit[++len]=x%B,x/=B;
return dfs(len,0,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
X=read(),Y=read(),K=read(),B=read();
if(B==1){printf("%d
",(int)(K>=X&&K<=Y));return 0;}
printf("%d
",calc(Y)-calc(X-1));
return 0;
}