# 【LG2567】[SCOI2010]幸运数字

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int tot;
unsigned long long L, R, t[3000000];
bool vis[3000];
vector<unsigned long long> num;
unsigned long long fac[1000];
int siz;
void dfs(int x, unsigned long long res) {
if (res <= R && res) num.push_back(res);
if (x == tot + 1) return ;
dfs(x + 1, res * 10 + 6);
dfs(x + 1, res * 10 + 8);
}
long long solve() {
long long ans = 0;
for (register int s = 1, l = min(siz, 19); s < 1 << l; s++) {
unsigned long long lcm = 0; int tt = 0;
for (register int i = 0; i < l; i++)
if (s >> i & 1) {
if (lcm) lcm = lcm * fac[i] / __gcd(fac[i], lcm);
else lcm = fac[i];
++tt;
}
ans += (tt & 1 ? 1 : -1) * (R / lcm - (L - 1) / lcm);
}
if (siz <= 19) return ans;
tot = 0;
for (int i = 19; i < siz; i++)
for (unsigned long long j = R / fac[i] * fac[i]; j >= L; j -= fac[i])
t[++tot] = j;
sort(&t[1], &t[tot + 1]);
tot = unique(&t[1], &t[tot + 1]) - t - 1;
for (int i = 1; i <= tot; i++)  {
bool flag = 1;
for (int j = 0; j < 19 && flag; j++)
if (t[i] % fac[j] == 0) flag = 0;
ans += flag;
}
return ans;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
cin >> L >> R;
long long tmp = R;
while (tmp) ++tot, tmp /= 10;
dfs(1, 0);
sort(num.begin(), num.end());
for (int i = 0; i < (int)num.size(); i++) {
if (vis[i]) continue;
fac[siz++] = num[i];
for (int j = i + 1; j < (int)num.size(); j++)
if (num[j] % num[i] == 0) vis[j] = 1;
}
printf("%lld\n", solve());
return 0;
}

posted @ 2019-10-30 15:24  heyujun  阅读(51)  评论(0编辑  收藏