Lucas模板
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i) #define rep(i,a,b) for(int i(a); i <= (b); ++i) #define dec(i,a,b) for(int i(a); i >= (b); --i) #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int N = 1000000 + 10; const int M = 10000 + 10; const int Q = 1000 + 10; const int A = 30 + 1; typedef long long LL; LL fac[N]; LL n, m, mod; inline LL Pow(LL a, LL b, int Mod){ LL ret(1); for (; b; b >>= 1, (a *= a) %= Mod) if (b & 1) (ret *= a) %= Mod; return ret;} inline LL C(LL n, LL m){ return m > n ? 0 : fac[n] * Pow(fac[m] * fac[n - m] % mod, mod - 2, mod) % mod; } LL Lucas(LL n, LL m){ return m == 0 ? 1 : (C(n % mod, m % mod) % mod) * (Lucas(n / mod, m / mod) % mod) % mod; } int main(){ scanf("%lld %lld %lld", &n, &m, &mod); fac[0] = 1; rep(i, 1, N - 5) fac[i] = (fac[i - 1] * i) % mod; printf("%lld\n", Lucas(n, m)); return 0; }
来自cxhscst2,(~ ̄▽ ̄)~