标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2973 Accepted Submission(s): 1363
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 25; 5 LL dp[maxn][maxn][2005]; 6 int b[maxn]; 7 LL dfs(int p,int pivot,int sum,bool flag){ 8 if(!p) return sum == 0; 9 if(sum < 0) return 0; 10 if(flag && dp[p][pivot][sum] != -1) return dp[p][pivot][sum]; 11 LL ret = 0; 12 int u = flag?9:b[p]; 13 for(int i = 0; i <= u; ++i) 14 ret += dfs(p-1,pivot,sum + (p - pivot)*i,flag||(i < u)); 15 if(flag) dp[p][pivot][sum] = ret; 16 return ret; 17 } 18 LL solve(LL x){ 19 int len = 0; 20 while(x){ 21 b[++len] = x%10; 22 x /= 10; 23 } 24 LL ret = 0; 25 for(int i = 1; i <= len; ++i) 26 ret += dfs(len,i,0,false); 27 return ret - len + 1; 28 } 29 int main(){ 30 int kase; 31 memset(dp,-1,sizeof dp); 32 scanf("%d",&kase); 33 while(kase--){ 34 LL L,R; 35 scanf("%I64d%I64d",&L,&R); 36 printf("%I64d\n",solve(R) - solve(L - 1)); 37 } 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4782270.html