P2567 [SCOI2010] 幸运数字

#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';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值