hdu4734 题解

链接:hdu4734

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,l,n) for(register int i=l;i<n;++i)
using namespace std;
const int N=4600;
int a[10],dp[10][N];
int n;

int (int pos,int limit,int sum){
if(sum<0)return 0;
if(pos==-1)return 1;
if(!limit&&dp[pos][sum]!=-1)return dp[pos][sum];
int up=limit?a[pos]:9;
int tmp=0;
rep(i,0,up+1){
tmp+=dfs(pos-1,limit&&i==a[pos],sum-i*(1<<pos));
}
if(!limit)dp[pos][sum]=tmp;
return tmp;
}

int solve(int x,int y){
int cnt=0,len=0,ans=0;
while(y){
ans+=y%10*(1<<len);
len++;
y/=10;
}
while(x){
a[cnt++]=x%10;
x/=10;
}
return dfs(cnt-1,1,ans);
}

int main(){
scanf("%d",&n);
memset(dp,-1,sizeof(dp));
rep(i,0,n){
int A,B;
scanf("%d%d",&A,&B);
printf("Case #%d: %dn",i+1,solve(B,A));
}
return 0;
}