首页 > 代码库 > HDU 4389 X mod f(x)
HDU 4389 X mod f(x)
X mod f(x)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
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
Sample Output
Case 1: 10
Case 2: 3
题意:求区间[l,r] 满足x%f[x]==0的数的个数!!
比较经典的数位动规!
纯枚举肯定会超时!!!
所以这里是通过枚举位数和求的!!!
AC代码如下:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int num[22]; int dp[11][82][82][82]; int mo; int dfs(int pos,int mo,int sum1,int sum2,bool limit) { int i; if(pos==-1) return sum1==mo&&sum2==0; if(!limit&&dp[pos][mo][sum1][sum2]!=-1) return dp[pos][mo][sum1][sum2]; int end = limit ? num[pos] : 9; int sum = 0; for(i = 0; i <= end; i++) { sum+=dfs(pos-1,mo,sum1+i,(sum2*10+i)%mo,limit&&i==end); } return limit ? sum : dp[pos][mo][sum1][sum2] = sum; } int solve (int n) { int pos=0; int ans=0; int i; while(n>0) { num[pos++]=n%10; n/=10; } for(i=1;i<=81;i++) { ans+=dfs(pos-1,i,0,0,true); } return ans; } int main() { int t; int l,r; int cas=1; scanf("%d",&t); memset(dp,-1,sizeof dp); while(t--) { scanf("%d%d",&l,&r); printf("Case %d: %d\n",cas++,solve(r)-solve(l-1)); } return 0; }
HDU 4389 X mod f(x)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。