bzoj 1485 卡特兰数 + 分解因子

思路:打表可以看出是卡特兰数,但是模数不一定是素数,所以需要分解一下因数。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>

using namespace std;

const int N = 2e6 + 7;
const int M = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +7;

int n, MOD, tot, p[N], mn[N], cnt[N];

LL fastPow(LL a, LL b) {
    LL ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % MOD;
        a = a * a % MOD; b >>= 1;
    }
    return ans;
}
void init() {
    for(int i = 2; i <= 2 * n; i++) {
        if(mn[i]) continue;
         p[++tot] = i; mn[i] = tot;
        for(int j = 2 * i; j <= 2 * n; j += i) {
            if(!mn[j]) {
                mn[j] = tot;
            }
        }
    }
}

void getCnt(int x, int op) {
    while(x != 1) {
        cnt[p[mn[x]]] += op;
        x /= p[mn[x]];
    }
}

int main() {

    scanf("%d%d", &n, &MOD);
    init();

    for(int i = 2; i <= 2 * n; i++) getCnt(i, 1);
    for(int i = 2; i <= n; i++) getCnt(i, -1);
    for(int i = 2; i <= n + 1; i++) getCnt(i, -1);

    LL ans = 1;
    for(int i = 2; i <= 2 * n; i++) ans = ans * fastPow(i, cnt[i]) % MOD;
    printf("%lld
", ans);
    return 0;
}


/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/9380574.html