定义美丽数n:n可以被它的每一位数字整除。
题意:问你在区间[L, R]里面的美丽数个数。
思路:1-9的lcm为2520。
设置dp[i][j][k]表示 整数位长度为i、当前整数对2520取余为j,前i位lcm为k的合法数字。
这样会MLE,离散化下合法的lcm就可以了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (100000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a, int b){
return a / gcd(a, b) * b;
}
int rec[2600];
LL dp[30][2520][50];
int Newyu(int s, int bit){
return (s * 10 + bit) % 2520;
}
int Newlcm(int s, int bit){
return bit == 0 ? s : lcm(s, bit);
}
void Init()
{
int cnt = 0;
for(int i = 1; i <= 2520; i++)
if(2520 % i == 0)
rec[i] = cnt++;
}
int bit[30];
LL DFS(int pos, int nowyu, int nowlcm, bool yes, bool zero)
{
if(pos == -1) return nowyu % nowlcm == 0;
if(!yes && dp[pos][nowyu][rec[nowlcm]] != -1) return dp[pos][nowyu][rec[nowlcm]];
LL ans = 0;
int End = yes ? bit[pos] : 9;
for(int i = 0; i <= End; i++)
ans += DFS(pos-1, zero&&i==0 ? 0 : Newyu(nowyu, i), zero&&i==0 ? 1 : Newlcm(nowlcm, i), yes&&i==End, zero&&i==0);
if(!yes) dp[pos][nowyu][rec[nowlcm]] = ans;
return ans;
}
LL Count(LL n)
{
int len = 0;
while(n)
{
bit[len++] = n % 10;
n /= 10;
}
return DFS(len-1, 0, 1, 1, 1);
}
int main()
{
Init(); CLR(dp, -1);
int t; Ri(t);
W(t)
{
LL n, m;
scanf("%I64d%I64d", &n, &m);
printf("%I64d\n", Count(m) - Count(n-1));
}
return 0;
}