数位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 <= 10 9), 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 <= 10 9), 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\n",cas++,calu(b)-calu(a-1)); } return 0; } |
本文探讨了数位DP(Digit DP)的概念及其应用,并通过具体实例展示了如何解决涉及时间限制和内存限制的问题。文章详细介绍了模运算在数位DP中的作用,以及如何在给定区间内计算满足特定条件的整数数量。通过逐步解析样例输入和输出,读者可以深入理解算法的实现细节和优化策略。

415

被折叠的 条评论
为什么被折叠?



