#include <bits/stdc++.h>
#define int long long
using namespace std;
int tot, a[5050], ret, l, r;
void dfs(int x) {
if (x > r) return;
if (x) a[ ++ tot] = x;
dfs(x * 10 + 6);
dfs(x * 10 + 8);
}
const int mod = 1000000007;
bool check(int a, int b) {
int A = a / mod, B = b / mod;
if (A * B) return true;
return a * b > r;
}
void calc(int x, int s, int cnt) {
if (x > tot && s != 1) {
if (cnt & 1) ret += r / s - l / s;
else ret -= r / s - l / s;
return;
}
if (x > tot) return;
calc(x + 1, s, cnt);
int d = a[x] / __gcd(s, a[x]);
if (!check(s, d)) calc(x + 1, s * d, cnt + 1);
}
bool vis[5050];
int Solve() {
dfs(0);
sort(&a[1], &a[tot + 1]);
int t = 0;
for (int i = 1; i <= tot; i ++ )
for (int j = 1; j < i; j ++ )
if (a[i] % a[j] == 0) {
vis[i] = true;
break;
}
for (int i = 1; i <= tot; i ++ )
if (!vis[i]) {
if (a[i] <= r / 2) a[ ++ t] = a[i];
else ret += r / a[i] - l / a[i];
}
tot = t;
reverse(&a[1], &a[tot + 1]);
calc(1, 1, 0);
return ret;
}
signed main() {
cin >> l >> r;
-- l;
cout << Solve() << '\n';
}
P2567 [SCOI2010] 幸运数字
于 2023-08-23 07:43:45 首次发布