数位DP........
X mod f(x)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1403 Accepted Submission(s): 599
Problem Description
Here is a function f(x):
int f ( int x ) {
if ( x == 0 ) return 0;
return f ( x / 10 ) + x % 10;
}
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.
int f ( int x ) {
if ( x == 0 ) return 0;
return f ( x / 10 ) + x % 10;
}
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.
Input
The first line has an integer T (1 <= T <= 50), indicate the number of test cases.
Each test case has two integers A, B.
Each test case has two integers A, B.
Output
For each test case, output only one line containing the case number and an integer indicated the number of x.
Sample Input
2
1 10
11 20
1 10
11 20
Sample Output
Case 1: 10
Case 2: 3
Case 2: 3
Author
WHU
Source
Recommend
zhuyuanchen520
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int bit[10],dp[10][82][82][82]; int dfs(int pos,int sum,int mod,int res,bool limit) { if(mod>81||sum>mod) return 0; if(pos==-1) return sum==mod&&res==0; if(!limit&&~dp[pos][sum][mod][res]) return dp[pos][sum][mod][res]; int end=limit?bit[pos]:9,ans=0; for(int i=0;i<=end;i++) { ans+=dfs(pos-1,sum+i,mod,(res*10+i)%mod,limit&&i==end); } if(!limit) dp[pos][sum][mod][res]=ans; return ans; } int calu(int x) { int pos=0,ans=0;; while(x) { bit[pos++]=x%10; x/=10; } for(int i=1;i<=pos*9;i++) { ans+=dfs(pos-1,0,i,0,true); } return ans; } int main() { int t,a,b,cas=1; memset(dp,-1,sizeof(dp)); scanf("%d",&t); while(t--) { scanf("%d%d",&a,&b); printf("Case %d: %d ",cas++,calu(b)-calu(a-1)); } return 0; } |