P为质
1 const int mod = 1000000009; 2 long long quickpow(long long a, long long b) { 3 if (b < 0) return 0; 4 long long ret = 1; 5 a %= mod; 6 while(b) { 7 if (b & 1) ret = (ret * a) % mod; 8 b >>= 1; 9 a = (a * a) % mod; 10 } 11 return ret; 12 } 13 long long inv(long long a) { 14 return quickpow(a, mod - 2); 15 }
N,P互质
1 ll extend_gcd(ll a, ll b, ll &x, ll &y) { 2 if (b == 0) { 3 x = 1, y = 0; 4 return a; 5 } 6 else { 7 ll r = extend_gcd(b, a % b, y, x); 8 y -= x * (a / b); 9 return r; 10 } 11 } 12 ll inv(ll a, ll n) { 13 ll x, y; 14 extend_gcd(a, n, x, y); 15 x = (x % n + n) % n; 16 return x; 17 }
逆元筛
1 const int mod = 1000000009; 2 const int maxn = 10005; 3 int inv[maxn]; 4 inv[1] = 1; 5 for(int i = 2; i < 10000; i++) 6 inv[i] = inv[mod % i] * (mod - mod / i) % mod;
阶乘逆元筛(阶乘数组:fac[ ])
1 fac[0]=fac[1]=1; 2 3 for(int i=2;i<=MAXN;i++)fac[i]=fac[i-1]*i%mod; 4 5 inv[MAXN]=quipow(fac[MAXN],mod-2); 6 7 for(int i=MAXN-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;