标签(空格分隔): 数位DP
#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;
}
const int p2[]={1,2,4,8,16,32,64,128,256,512,1024,2048};
int T,A,B,dit[15],f[15][10240];
int dp(int step,int r,int lim)
{
if(step==0)return 1;
if(!lim&&f[step][r]!=-1)return f[step][r];
int x=lim?dit[step]:9,res=0;
for(int i=0;i<=x;i++)
{
if(r<p2[step-1]*i)continue;
res+=dp(step-1,r-p2[step-1]*i,lim&&i==x);
}
if(!lim)f[step][r]=res;
return res;
}
int main()
{
memset(f,-1,sizeof(f));
T=read();
for(int kase=1;kase<=T;kase++)
{
A=read(),B=read();
int len=0,x=0,b=1;
while(B)dit[++len]=B%10,B/=10;
while(A)x+=A%10*b,A/=10,b*=2;
printf("Case #%d: %d
",kase,dp(len,x,1));
}
return 0;
}