题意:
思路:
数位
d
p
dp
dp挺经典的一个题辣,有一个很明显的状态就是
f
[
p
o
s
]
[
n
u
m
]
[
l
c
m
]
f[pos][num][lcm]
f[pos][num][lcm]表示到了第
p
o
s
pos
pos位,数是
n
u
m
num
num,个位数最小公倍数
l
c
m
lcm
lcm。直接这样写肯定炸了,考虑如何优化。
由于
[
1
,
9
]
[1,9]
[1,9]所有数的最小公倍数是
2520
2520
2520,所以存一个
n
u
m
m
o
d
2520
num\bmod 2520
nummod2520即可,这样空间就变成了
20
∗
2520
∗
2520
20*2520*2520
20∗2520∗2520,看似能过,其实不然,还有10组样例呢,铁
T
T
T,再考虑优化。
注意到,
l
c
m
lcm
lcm的值域不会很大,我们映射一下就会发现只有
48
48
48个值,这样就变成了
20
∗
2520
∗
48
20*2520*48
20∗2520∗48,应该是可以跑过去了。
但是我实现的时候又发现了一点小问题,就是如果记
f
[
p
o
s
]
[
n
u
m
]
[
l
c
m
]
[
f
l
a
g
]
f[pos][num][lcm][flag]
f[pos][num][lcm][flag]的话会
T
L
E
11
TLE11
TLE11,可能是因为常数比较大,每次都需要情空,所以还是
f
[
p
o
s
]
[
n
u
m
]
[
l
c
m
]
f[pos][num][lcm]
f[pos][num][lcm]存比较好。
// Problem: D. Beautiful numbers
// Contest: Codeforces - Codeforces Beta Round #51
// URL: https://codeforces.com/problemset/problem/55/D
// Memory Limit: 256 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
LL l,r;
LL f[20][2600][60];
vector<int>v;
int a[20],tot;
int mp[N];
int find(int x) {
return lower_bound(v.begin(),v.end(),x)-v.begin();
}
int lcm(int a,int b) {
if(!b) return a;
else return a/__gcd(a,b)*b;
}
LL dp(int pos,int num,int pre,int flag) {
if(pos==0) return num%pre==0? 1:0;
if(f[pos][num][mp[pre]]!=-1&&flag) return f[pos][num][mp[pre]];
int x=flag? 9:a[pos];
LL ans=0;
for(int i=0;i<=x;i++) {
ans+=dp(pos-1,(num*10+i)%2520,lcm(pre,i),flag||i<x);
}
if(flag) f[pos][num][mp[pre]]=ans;
return ans;
}
LL solve(LL x) {
tot=0;
while(x) a[++tot]=x%10,x/=10;
return dp(tot,0,1,0);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
for(int i=0;i<1<<9;i++) {
int now=-1;
for(int j=0;j<9;j++)
if(i>>j&1) {
if(now==-1) now=j+1;
else now=now*(j+1)/__gcd(now,j+1);
}
v.pb(now);
}
sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());
int tot=0;
for(auto x:v) mp[x]=++tot;
memset(f,-1,sizeof(f));
int _; scanf("%d",&_);
while(_--) {
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-1));
}
return 0;
}
/*
*/