数位DP!!!
代码如下:
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<iomanip> 5 #include<cmath> 6 #include<cstring> 7 #include<vector> 8 #define ll __int64 9 #define pi acos(-1.0) 10 #define MAX 50000 11 using namespace std; 12 ll dp[22][15][200]; 13 int bit[22]; 14 ll dfs(int pos,int mod,int sum,bool f) 15 { 16 if(pos==-1) return mod==0; 17 if(!f&&dp[pos][mod][sum]!=-1) return dp[pos][mod][sum]; 18 ll ans=0; 19 int e=f?bit[pos]:9; 20 for(int i=0;i<=e;i++){ 21 ans+=dfs(pos-1,(mod+i)%10,sum+i,f&&i==e); 22 } 23 if(!f) dp[pos][mod][sum]=ans; 24 return ans; 25 } 26 ll solve(ll n) 27 { 28 int m=0; 29 while(n){ 30 bit[m++]=n%10; 31 n/=10; 32 } 33 return dfs(m-1,0,0,1); 34 } 35 int main(){ 36 int t,ca=0; 37 ll a,b; 38 memset(dp,-1,sizeof(dp)); 39 scanf("%d",&t); 40 while(t--){ 41 scanf("%I64d%I64d",&a,&b); 42 if(a>b) swap(a,b); 43 printf("Case #%d: %I64d ",++ca,solve(b)-solve(a-1)); 44 } 45 return 0; 46 }