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.
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
题意:求x%f(x)等于0的x的个数f(x)求的是x每一位数的和
由于最大范围是1e9,当999999999时f(x)最大等于81,所以枚举每一个f(x),就当做81次普通的数位dp来做;
具体看代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int a[20];
int dp[10][90][90][90];
int dfs(int pos,int sta,int sx,int lead,int limit)
{
if(pos<0){
if(lead==sx&&!sta)return 1;
return 0;
}
if(!limit&&dp[pos][sx][lead][sta]!=-1)return dp[pos][sx][lead][sta];
int up=limit?a[pos]:9;
int ans=0;
for(int i=0;i<=up;i++)
{
ans += dfs(pos - 1, (sta * 10 + i)%sx,sx,(lead + i), limit && i == up);
}
if(!limit) dp[pos][sx][lead][sta]=ans;
return ans;
}
int solve(int x)
{
int t=0;
while(x)
{
a[t++]=x%10;
x/=10;
}
int ans=0;
for(int i=1;i<=81;i++)
ans+=dfs(t-1,0,i,0,1);
return ans;
}
int main()
{
int T,t=1;
memset(dp,-1,sizeof(dp));
cin>>T;
while(T--)
{
int l,r;
cin>>l>>r;
printf("Case %d: %d\n",t++,solve(r)-solve(l-1));
}
return 0;
}