能注意到1~9的最小公倍数是2520,
所以我们只需要维护一下到dex位前的数字组成的lcm,和num
num需要控制在2520之内,因为2520是lcm的整数倍
所以num%lcm=(num%2520)%lcm
所以num=(num*10+i)%2520;
最后只需要判断num是否被lcm整除就可以了
但是DP[20][2555][2555] 是要超内存的
所以我们需要离散一下第二维,因为1~9组成的lcm的数量其实很有限(思考怎么算出所有的lcm)
所以需要新建Hash[50]数组压DP[20][50][2555];
还要此题有一个最坑点!!!! DP初始化要为-1,如果初始为0就算重了
因为DP[][][]本身有可能为0
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#include<time.h>
#define INF 0x3f3f3f3f
#define LL long long
#define fora(i,a,n) for(int i=a;i<=n;i++)
#define fors(i,n,a) for(int i=n;i>=a;i--)
#define sci(x) scanf("%d",&x);
#define scl(x) scanf("%lld",&x);
#define MAXN 100024
#define MOD 2520
const double eps = 1e-8;
using namespace std;
LL t, l, r, k;
LL digits[22], dp[22][50][2555];
LL Hash[2555], v = 0;
void hhash() {
for (int i = 1; i <= 2520 / 2; i++) {
if (2520 % i == 0) {
Hash[i] = ++v;
}
}
}
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a%b);
}
LL dfs(LL dex, LL lcm, LL num, LL p) {
LL sum = 0;
if (dex == 0) return num%lcm == 0;
if (dp[dex][Hash[lcm]][num]!=-1 && !p) return dp[dex][Hash[lcm]][num];
LL u = p ? digits[dex] : 9;
fora(i, 0, u) {
LL Lcm = lcm;
if (i) Lcm = lcm*i / gcd(lcm, i);
sum += dfs(dex - 1, Lcm, (num * 10 + i) % MOD, p&&i == u);
}
if (!p) dp[dex][Hash[lcm]][num] = sum;
return sum;
}
LL digit(LL num) {
LL nnum = num;
k = 0;
while (num) {
digits[++k] = num % 10;
num /= 10;
}
return dfs(k, 1, 0, 1);
}
int main() {
#ifdef DID
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
/*clock_t s = clock();*/
scl(t);
memset(dp, -1, sizeof(dp));
hhash();
while (t--) {
scl(l); scl(r);
LL num1 = digit(r);
LL num2 = digit(l - 1);
printf("%lld\n", num1 - num2);
}
/*clock_t e = clock();
printf("执行完此代码需要%lfs\n", double(e - s) / 1000);*/
return 0;
}