这个题因为总和加起来是比较小的9*9 = 81 这样可以保留前面枚举的数对所有的可能出现的和的余数,然后依次向下找。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 100000 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 int dp[10][82][82][82]; 18 int d[10]; 19 int dfs(int i,int e,int sum,int m,int r) 20 { 21 if(i==-1) 22 { 23 return r==0&&(sum==m); 24 } 25 if(!e&&dp[i][sum][m][r]!=-1) return dp[i][sum][m][r]; 26 int mk = e?d[i]:9; 27 int ans = 0; 28 for(int j = 0 ; j <= mk ; j++) 29 { 30 ans+=dfs(i-1,e&&j==mk,sum+j,m,(r*10+j)%m); 31 } 32 return e?ans:dp[i][sum][m][r] = ans; 33 } 34 int cal(int x) 35 { 36 int g = 0; 37 if(x<=0) return 0; 38 while(x) 39 { 40 d[g++] = x%10; 41 x/=10; 42 } 43 int ans = 0; 44 for(int i = 1 ; i <= 81 ; i++) 45 { 46 47 int k = dfs(g-1,1,0,i,0); 48 ans+=k; 49 } 50 return ans; 51 } 52 int main() 53 { 54 int t,a,b; 55 cin>>t; 56 int kk = 0; 57 memset(dp,-1,sizeof(dp)); 58 while(t--) 59 { 60 cin>>a>>b; 61 printf("Case %d: ",++kk); 62 cout<<cal(b)-cal(a-1)<<endl; 63 } 64 return 0; 65 }