#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=5e3+100;
const int mod=1e9+7;
char L[maxn],R[maxn];
int bit[maxn],p[maxn],m,n;
int dp[maxn][62][62];
int dfs(int len,int now,int pre,int op)
{
if(len==0) return now==0;
if(!op&&dp[len][now][pre]!=-1) return dp[len][now][pre];
int mx=op?bit[len]:9;
ll ans=0;
for(int i=0; i<=mx; i++)
{
int tp=((now+i*pre%m-i*p[len-1]%m)%m+m)%m;
ans+=dfs(len-1,tp,(pre+i)%m,op&&(i==mx));
//ans%=mod;
}
ans%=mod;
if(!op)dp[len][now][pre]=ans;
return ans;
}
int getsum(char *a)
{
//memset(dp,-1,sizeof dp);
n=strlen(a+1);
int len=0;
for(int i=n; i>=1; i--)
{
bit[++len]=a[i]-'0';
}
return dfs(len,0,0,1);
}
signed main()
{
IOS
int tt;
cin>>tt;
while(tt--)
{
cin>>(L+1)>>(R+1)>>m;
int mx=max(strlen(L+1),strlen(R+1));
for(int i=0; i<=mx+10; i++)
for(int j=0; j<=m; j++)
for(int k=0; k<=m; k++)
dp[i][j][k]=-1;
p[0]=1;
for(int i=1; i<=mx; i++)p[i]=p[i-1]*10%m;
mx=strlen(L+1);
for(int i=mx; i>=1; i--)
{
if(L[i]>'0')
{
L[i]=L[i]-1;
break;
}
else L[i]='9';
}
//cout<<(L+1)<<"\n";
int sumr=getsum(R);
int suml=getsum(L);
cout<<(ll)((ll)sumr-suml+mod)%mod<<"\n";
}
}
Junior Mathematician(数位dp)
最新推荐文章于 2024-11-02 16:10:25 发布