求助 wa 5#
查看原帖
求助 wa 5#
520056
luoyx楼主2023/3/23 18:19
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=25,M=2600,U=2520;
int t,L,R;
int dp[N][M][59];
int a[N],len;
int mp[59],cnt;

int gcd(int x,int y){
	if (!y) return x;
	return gcd(y,x%y);
} 

int dfs(int pos,int lcm,int mod,bool st,bool op){
	if(pos==0) return mod%lcm==0?1:0;
	if(!op&&dp[pos][mod][mp[lcm]]!=-1) return dp[pos][mod][mp[lcm]];
	int mx=op?a[pos]:9,res=0;
	for(int i=0;i<=mx;i++){
		res+=dfs(pos-1,lcm*max(i,(long long)1)/gcd(lcm,max(i,(long long)1)),(mod*10+i)%U,st&&!i,op&&(i==a[pos]));
	}
	return !op&&!st?dp[pos][mod][mp[lcm]]=res:res;
}

int calc(int x){
	len=0;
	while(x){
		a[++len]=x%10;
		x/=10;
	}
	memset(dp,-1,sizeof(dp));
	return dfs(len,1,0,1,1);
}

signed main(){
	cin>>t;
	for(int i=1;i<=2520;i++)
        if(2520%i==0) mp[i]=++cnt;
	while(t--){
		cin>>L>>R;
		cout<<calc(R)-calc(L-1)<<endl;
	}
}
2023/3/23 18:19
加载中...