跳到正文

题解

题解 P2567 【[SCOI2010]幸运数字】

xyz32768约 3 分钟
原文
文章信息与版本

文章信息

发布
2017年11月26日
收录
2026年5月9日
更新
2026年11月18日
篇幅
约 3 分钟
题目
P2567

版本

  1. v1当前

先预处理[1,1010][1,10^{10}]内的所有幸运号码。

对于一个幸运号码XX,在[A,B][A,B]中XX的倍数的个数,就是

⌊BX⌋−⌈AX⌉+1\lfloor\frac{B}{X}\rfloor-\lceil\frac{A}{X}\rceil+1。

然而,两个幸运号码对应的近似幸运号码可能有交集。考虑到这一点,就可以用容斥,也就是:

选11个幸运号码−-选22个幸运号码的lcm++选33个幸运号码的lcm−...-...

但是直接做是220462^{2046}的,啃腚会TLE。考虑33个剪枝:

1、发现对于两个幸运号码a,ba,b,如果a∣ba|b,那么对于所有的b∣xb|x就一定有a∣xa|x,因此这样的bb是不必要的。去掉所有这样的bb后,还剩下943943个幸运号码。 2、当前的lcm一旦大于BB就不再继续搜索。这样复杂度就能大大降低。

3、将预处理出的幸运号码从大到小排序,使lcm能更快地超越上界BB。

代码:

C++
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e4 + 5;
ll A, B, a[N], num[N], Ans;
int tot, n; bool mark[N];
void dfs1(int dep, int cnt, ll now, ll x) {
    if (dep > cnt) return (void) (a[++tot] = now);
    dfs1(dep + 1, cnt, now + x * 6, x * 10);
    dfs1(dep + 1, cnt, now + x * 8, x * 10);
}
void sieve() {
    int i, j;
    for (i = 1; i <= tot; i++) {
        if (!mark[i]) num[++n] = a[i];
        for (j = i + 1; j <= tot; j++)
            if (a[j] % a[i] == 0) mark[j] = 1;
    }
}
ll Cnt(ll l, ll r, ll PYZ) {
    l = l / PYZ + (l % PYZ != 0); r /= PYZ;
    return r - l + 1;
}
void dfs2(int dep, int cnt, ll val) {
    if (val > B) return;
    if (dep > n) {
        if (cnt == 0) return;
        Ans += Cnt(A, B, val) * ((cnt & 1) ? 1 : -1);
        return;
    }
    dfs2(dep + 1, cnt, val);
    ll tmp = val / __gcd(val, num[dep]);
    if (1.0 * tmp * num[dep] <= B)
        dfs2(dep + 1, cnt + 1, tmp * num[dep]);
}
bool comp(ll a, ll b) {return a > b;}
int main() {
    int i; cin >> A >> B;
    for (i = 1; i <= 10; i++) dfs1(1, i, 0, 1);
    sieve(); sort(num + 1, num + n + 1, comp);
    cout << (dfs2(1, 0, 1), Ans) << endl;
    return 0;
}
原文
存档评论19 条抓取于