CF 55 D. Beautiful numbers

时间:2023-03-09 06:59:29
CF 55 D. Beautiful numbers

D. Beautiful numbers

链接

题意:

  求[L,R]中多少个数字可以整除它们的每一位上的数字。

分析:

  要求模一些数字等于0等价于模它们的lcm等于0,所以可以记录当前出现的数字的lcm,最后判断组成的数字是否模lcm等于0。

  但是这个数字太大记录不下。根据一个性质a%b=(a%kb)%b,所以可以记录当前的数字模2520的值,最后模一下lcm。

  这样的状态是$20 \times 2520 \times 2520$的,状态太大了,考虑如何缩小空间。因为1~9的lcm只能是50个左右,所以可以将第三维压成50。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline LL read() {
LL x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
LL dp[][][N + ];
int num[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
int a[], pos[N + ]; int gcd(int a,int b) { return b == ? a : gcd(b, a % b); }
int getlcm(int a,int b) { return a * b / gcd(a, b); } LL dfs(int p,int lcm,int now,bool lim) {
if (p == ) return lcm && now % num[lcm] == ;
if (!lim && ~dp[p][lcm][now]) return dp[p][lcm][now];
int u = lim ? a[p] : ;
LL ans = ;
for (int i = ; i <= u; ++i) {
int t = lcm ? pos[getlcm(num[lcm], max(i, ))] : i;
ans += dfs(p - , t, (now * + i) % N, lim && i == u);
}
if (!lim) dp[p][lcm][now] = ans;
return ans;
}
LL solve(LL x) {
if (x <= ) return ;
int pos = ;
while (x) {
a[++pos] = x % ; x /= ;
}
return dfs(pos, , , );
}
int main() {
memset(dp, -, sizeof(dp));
for (int i = ; i < ; ++i) pos[num[i]] = i;
for (int T = read(); T --; ) {
LL x = read(), y = read();
cout << (solve(y) - solve(x - )) << "\n";
}
return ;
}