#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=2520;
int cnt,index[3000],num[20];
ll dp[20][mod][50];
void init()
{
memset(dp,-1,sizeof(dp));
for(int i=1;i<=mod;i++)
if(mod%i==0)
index[i]=cnt++;
}
int gcd(int a,int b)
{
return a%b?gcd(b,a%b):b;
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
ll dfs(int pos,int presum,int prelcm,bool flag)
{
if(pos<0)
return presum%prelcm==0;
ll tp=dp[pos][presum][index[prelcm]],ans=0;
if(!flag&&tp!=-1)
return tp;
int up=flag?num[pos]:9;
for(int i=0;i<=up;i++)
{
int newsum=(presum*10+i)%mod;
int newlcm=prelcm;
if(i)newlcm=lcm(prelcm,i);
ans+=dfs(pos-1,newsum,newlcm,flag&&(i==up));
}
if(!flag)
dp[pos][presum][index[prelcm]]=ans;
return ans;
}
ll cal(ll n)
{
int cnt=0;
while(n)
{
num[cnt++]=(int)(n%10);
n/=10;
}
return dfs(cnt-1,0,1,true);
}
int main()
{
init();
ll l,r;
int t;
scanf("%d",&t);
while(t--&&scanf("%lld%lld",&l,&r)!=EOF)
printf("%lld\n",cal(r)-cal(l-1));
return 0;
}
CF 55D beautiful number(数位dp)
最新推荐文章于 2021-10-13 21:25:21 发布