第一次做数位dp,套模板的,留着以后用。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define tp(x) printf("x = %d\n",x)
#define ansp(x) printf("%d\n",x)
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
#define LL long long
using namespace std;
typedef pair<int,int> pii;
const ll inf = 0x3f3f3f3f;
const int mod = 2520;

const int mx = 30;
int dig[mx],index[mod+2];
ll dp[20][mod][50];
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a%b);
}

LL lcm(LL a, LL b) {
return a/gcd(a, b)*b;
}
ll dfs(int pos, int presum, int prelcm, bool limit){
if(pos == -1)
return presum % prelcm == 0; //这里写错
if(!limit && dp[pos][presum][index[prelcm]] != -1)
return dp[pos][presum][index[prelcm]];
int n = limit?dig[pos]:9;
ll ans = 0;
for(int i = 0; i <= n; i++){
int nowlcm = prelcm;
int nowsum = (presum*10+i)%mod;
if(i)
nowlcm =lcm(prelcm,i);
ans += dfs(pos-1,nowsum,nowlcm,limit&& i == n);
}
if(!limit)
dp[pos][presum][index[prelcm]] = ans;
return ans;
}


ll calc( ll x){
int pos = 0;
while(x){
dig[pos++] = x%10;
x /= 10;
}
return dfs(pos-1,0,1,1);
}

int main(){
// freopen("F:\\in.txt","r",stdin);
int co = 0;
for(int i = 1; i <= mod; i++){
if(mod %i == 0)
index[i] = co++;
}
mem(dp,-1);
int T=10; scanf("%d",&T);
ll a,b;
while(T--){
cin>>a>>b;
cout<<(calc(b)-calc(a-1))<<endl;
}

return 0;
}