Lucas小记
组合数学全忘了
记笔记记笔记
做个简单题
代码 from bzoj4403
1 #include <stdio.h> 2 3 #define p 1000003 4 5 typedef long long ll; 6 7 ll fac[p], inv[p]; 8 9 ll C(ll x, ll y) { 10 if(x < y) return 0; 11 return fac[x] * inv[y] % p * inv[x - y] % p; 12 } 13 14 ll lucas(ll x, ll y) { 15 ll ret = 1; 16 while(x && y) { 17 ret = (ret * C(x % p, y % p)) % p; 18 x /= p, y /= p; 19 } 20 return ret; 21 } 22 23 int main() { 24 int t, n, l, r, i; 25 fac[0] = 1, inv[1] = inv[0] = 1; 26 for(i = 2;i < p;i ++) inv[i] = inv[p % i] * (p - p / i) % p; 27 for(i = 2;i < p;i ++) inv[i] = inv[i - 1] * inv[i] % p; 28 for(i = 1;i < p;i ++) fac[i] = fac[i - 1] * i % p; 29 for(scanf("%d", &t);t --;) { 30 scanf("%d %d %d", &n, &l, &r), r -= l - 1; 31 printf("%lld\n", (lucas(n + r, r) + p - 1) % p); 32 } 33 return 0; 34 }
1. nm大p小用lucas
2.p固定,可以预处理
可以选择只预处理fac前缀,O(logn)求组合数
也可以同时预处理inv前缀,O(1)求组合数
3.inv[0] = inv[1] = 1
inv[i] = inv[p % i] * (p - p / i) % p