hdu3709 题解

链接:hdu3709

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
50
51

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

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


ll solve(ll x){
int cnt=0;
while(x){
a[cnt++]=x%10;
x/=10;
}
ll ans=0;
rep(i,0,cnt){
ans+=dfs(cnt-1,i,1,0);
}
return ans-cnt+1;
}

int main(){
ll l,r;
int T;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--){
scanf("%lld%lld",&l,&r);
printf("%lldn",solve(r)-solve(l-1));
}
return 0;
}