标签:
Here is a function f(x):
1 int f(int x) { 2 if(x == 0) return 0; 3 return f(x/10) + x%10; 4 }
Now, you want to know, in a given interval [A, B] (1 <= A <= B <= 109), how many integer x that mod f(x) equal to 0.
2 1 10 11 20
Case 1: 10 Case 2: 3
1 #include <bits/stdc++.h> 2 using namespace std; 3 int b[20],dp[20][90][90],ans[60];//长度,余数,位和 4 int dfs(int len,int mod,int left,int sum,bool flag) { 5 if(!len) return left == 0 && sum == mod; 6 if(flag && dp[len][left][sum] != -1) return dp[len][left][sum]; 7 int u = flag?9:b[len],ret = 0; 8 for(int i = 0; i <= u; ++i) 9 ret += dfs(len-1,mod,(left*10 + i)%mod,sum + i,flag||i < u); 10 if(flag) dp[len][left][sum] = ret; 11 return ret; 12 } 13 int solve(int x,int cur) { 14 int len = 0; 15 while(x) { 16 b[++len] = x%10; 17 x /= 10; 18 } 19 return dfs(len,cur,0,0,false); 20 } 21 int main() { 22 int kase,cs,x[60],y[60]; 23 scanf("%d",&kase); 24 for(cs = 0; cs < kase; ++cs) 25 scanf("%d%d",x + cs,y + cs); 26 for(int i = 1; i <= 81; ++i) { 27 memset(dp,-1,sizeof dp); 28 for(int j = 0; j < kase; ++j) 29 ans[j] += solve(y[j],i) - solve(x[j]-1,i); 30 } 31 for(cs = 0; cs < kase; ++cs) 32 printf("Case %d: %d\n", cs + 1, ans[cs]); 33 return 0; 34 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4800670.html