【LOJ】#2544. 「JXOI2018」游戏

题解

九条可怜还有那么善良的一面???

显然有些数在这个区间里没有数是它的约数,它们其中的最后一个取的一定就是\(t(p)\)的值
这样我们只需要枚举\(t(p)\)的值,这个值就是“没有任何数是自己的约数”最后出现的位置

假如这个位置是\(k\),总共“没有任何数是自己的约数”有\(tot\)个,我们选择第\(k\)个位置,以及在之前\(k - 1\)个位置里选\(tot - 1\)个位置
\(\binom{k - 1}{tot - 1}\)然后这\(tot\)个数可以随意排列再乘上\(tot!\),剩下的没有限制的数随意排列就再乘一个\((N - tot)!\)
最后统计进答案的时候乘上\(k\)

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
//#define ivorysi
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mo 974711
#define MAXN 15005
#define RG register
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
        while(c < '0' || c > '9') {
            if(c == '-') f = -1;
            c = getchar();
        }
        while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {putchar('-');x = -x;}
    if(x >= 10) {
        out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int L,R,N;
bool vis[10000005];
int fac[10000005],invfac[10000005],tot;
int mul(int a,int b) {return 1LL * a * b % MOD;}
int inc(int a,int b) {a = a + b;if(a >= MOD) a -= MOD;return a;}
int fpow(int x,int c){
    int res = 1,t = x;
    while(c) {
        if(c & 1) res = mul(res,t);
        t = mul(t,t);
        c >>= 1;
    }
    return res;
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(mul(fac[n],invfac[m]),invfac[n - m]);
}
void Solve() {
    read(L);read(R);
    N = R - L + 1;
    fac[0] = 1;
    for(int i = 1 ; i <= N ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[N] = fpow(fac[N],MOD - 2);
    for(int i = N - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    for(int i = L ; i <= R ; ++i) {
        if(!vis[i]) {
            ++tot;
            int t = i;
            while(t <= R) vis[t] = 1,t += i;
        }
    }
    int ans = 0;
    for(int i = tot ; i <= N ; ++i) {
        int t = mul(C(i - 1,tot - 1),fac[tot]);
        t = mul(t,fac[N - tot]);
        ans = inc(ans,mul(t,i));
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ivorysi/p/9120486.html