lgB3717 計算組合數

chenfy27發表於2024-03-10

給出T次詢問,每次給出n和m,求C(n,m)對998244353取模的結果。為了避免輸出太多內容,只需要輸出所有查詢結果的異或和。
1<=T<=5E6; 0<=m<=n<=5E6

先O(n)預處理出所有數的階乘及其對應的乘法逆元,然後O(1)處理每次詢問。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define per(i,a,b) for(int i=b; i>=a; i--)

const int MOD = 998244353;
const int Z = 5000005;
int T, N, fac[Z], ifac[Z];
int comb(int n, int m) {
    return fac[n] * ifac[m] % MOD * ifac[n-m] % MOD;
}
int power(int a, int b) {
    int r = 1;
    for (int t = a; b; b /= 2) {
        if (b & 1) r = r * t % MOD;
        t = t * t % MOD;
    }
    return r % MOD;
}
void solve() {
    cin >> T >> N;
    fac[0] = 1;
    rep(i,1,N) fac[i] = fac[i-1] * i % MOD;
    ifac[N] = power(fac[N], MOD-2);
    per(i,0,N-1) ifac[i] = ifac[i+1] * (i+1) % MOD;
    int ans = 0;
    while (T--) {
        int n, m;
        cin >> n >> m;
        ans ^= comb(n, m);
    }
    cout << ans << "\n";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

相關文章