Junior Mathematician(数位dp)

Posted thusloop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Junior Mathematician(数位dp)相关的知识,希望对你有一定的参考价值。

Junior Mathematician

#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)的主要内容,如果未能解决你的问题,请参考以下文章

FZU - 2042 The Mad Mathematician(数位dp)

I - The Mad Mathematician FZU - 2042(未解决)

数位dp

夜深人静写算法(二十九)- 数位DP

数位dp模板 [dp][数位dp]

数位DP入门