题目链接:点击查看 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
题解:数位dp模板题,注意到位数和最多为81,暴力枚举一下膜数即可。
dp[len][sum_w][mod][cnt] 表示长度为len,目前位数和为sum_w,膜数位mod,cnt为前面膜剩下的余数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[12][82][82][82];
int w[12];
int dfs(int len,int sum_w,int mod,int cnt,int limit)
{
if(len<0) return sum_w==mod&&cnt==0;
if(limit==0&&dp[len][sum_w][mod][cnt]!=-1) return dp[len][sum_w][mod][cnt];
int ans=0;
int tmp=limit?w[len]:9;
for(int i=0;i<=tmp;i++)
ans+=dfs(len-1,sum_w+i,mod,(cnt*10+i)%mod,limit&&i==tmp);
return limit? ans:dp[len][sum_w][mod][cnt]=ans;
}
int solve(int x)
{
int len=0;
while(x)
{
w[len++]=x%10;
x=x/10;
}
int ans=0;
for(int i=1;i<=81;i++)
{
ans+=dfs(len-1,0,i,0,1);
// cout<<ans<<endl;
}
return ans;
}
int main()
{
memset(dp,-1,sizeof(dp));
int T,nn=1;
int l,r;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&l,&r);
printf("Case %d: %d\n",nn++,solve(r)-solve(l-1));
}
return 0;
}