【题意】
给定a,b,p求出
the number of integers in the range [A, B] which is divisible by p and the sum of its digits is also divisible by p.
1 <= A <= B < 2^31 and 0<p<10000.
【题解】
这个题最大的坑人之处在于,在p>=100时答案一定是0!!(各位数之和最大不超过100)
然后设计状态进行数位DP即可。
f[i][j][k]表示i位数,这个数mod p为j,各位数之和mod p为k的方案数。
【Coding】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//keng
long long f[12][101][101];
long long pow[15],num[15];
long long a,b,p;
long long dfs(int l,int k1,int k2,int e)
{
if(l==-1) return (k1==0)&&(k2==0);
if(!e && f[l][k1][k2]!=-1)return f[l][k1][k2];
int u=(e?num[l]:9);
long long res=0;
for(int i=0;i<=u;i++)
res+=dfs(l-1,(pow[l]*i+k1)%p,(i+k2)%p,e&&i==u);
return e?res:f[l][k1][k2]=res;
}
long long solve(long long x)
{
int l=0;
memset(f,-1,sizeof(f));
while(x!=0)
{
num[l++]=x%10;
x=x/10;
}
return dfs(l-1,0,0,1);
}
int main()
{
int sec;
scanf("%d",&sec);
{
long long x=1;
for(int i=0;i<=10;i++)
{
pow[i]=x;
x=x*10;
}
}
while(sec--)
{
scanf("%lld%lld%lld",&a,&b,&p);
if(p>=100)printf("0\n");else
{
long long ans=solve(b)-solve(a-1);
printf("%lld\n",ans);
}
}
return 0;
}