数位DP . dp[位][前缀和][总和][mod] 然后枚举总和1~81
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2426 Accepted Submission(s): 954
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
Author
WHU
/* ***********************************************
Author :CKboss
Created Time :2015年09月08日 星期二 21时45分37秒
File Name :HDOJ4389.cpp
************************************************ */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int fn,wei[30];
void fj(int x)
{
if(x==0) wei[0]=0,fn=1;
else
{
fn=0;
while(x)
{
wei[fn++]=x%10;
x/=10;
}
}
}
int dp[10][82][82][82];
int dfs(int pos,int presum,int sum,int mod,bool limit)
{
if(sum>81||presum>sum) return 0;
if(pos==-1)
{
return (presum==sum)&&(mod==0);
}
if(limit==false&&dp[pos][presum][sum][mod]!=-1)
return dp[pos][presum][sum][mod];
int ed=9;
if(limit==true) ed=wei[pos];
int ret=0;
for(int i=0;i<=ed;i++)
{
ret+=dfs(pos-1,presum+i,sum,(mod*10+i)%sum,(limit==true&&i==ed));
}
if(limit==false) dp[pos][presum][sum][mod]=ret;
return ret;
}
int Get(int x)
{
int ans=0;
fj(x);
for(int i=1;i<=fn*9;i++)
{
ans+=dfs(fn-1,0,i,0,1);
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
memset(dp,-1,sizeof(dp));
int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("Case %d: %d\n",cas++,Get(b)-Get(a-1));
}
return 0;
}