BZOJ5323:[JXOI2018]游戏

传送门
不难发现,所有不能被其他数筛掉的数是一定要选的,只有选了这些数字才能结束
假设有 \(m\) 个,枚举结束时间 \(x\),答案就是 \(\sum \binom{x-1}{m-1}m!(n-m)!x\)
埃氏筛法即可求出 \(m\)

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn(1e7 + 5);
const int mod(1e9 + 7);

inline void Inc(int &x, const int y) {
    x = x + y >= mod ? x + y - mod : x + y;
}

int l, r, n, m, ans, fac[maxn], inv[maxn];
bitset <maxn> vis;

int main() {
    int i, j, k;
    scanf("%d%d", &l, &r), n = r - l + 1;
    if (l == 1) {
        for (k = i = 1; i < n; ++i) k = (ll)k * i % mod;
        ans = (ll)n * (n + 1) / 2 % mod * k % mod;
        return printf("%d\n", ans), 0;
    }
    for (i = l; i <= r; ++i)
        if (!vis[i]) for (++m, j = i; j <= r; j += i) vis[j] = 1;
    inv[0] = inv[1] = fac[0] = fac[1] = 1;
    for (i = 2; i <= n; ++i) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
    for (i = 2; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % mod, inv[i] = (ll)inv[i] * inv[i - 1] % mod;
    for (i = m; i <= n; ++i) Inc(ans, (ll)fac[i] * inv[i - m] % mod);
    ans = (ll)ans * m % mod * fac[n - m] % mod, printf("%d\n", ans);
    return 0;
}
posted @ 2019-02-20 09:41 Cyhlnj 阅读(...) 评论(...) 编辑 收藏