标签:
题目链接:http://lightoj.com/volume_showproblem.php?problem=1067
模板求C(n,m)%p, Lucas模板;
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <map> #include <queue> #include <stack> #include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a)) #define N 1000953 #define INF 0x3f3f3f3f const int MOD = 1000003; typedef long long LL; LL f[N]; LL Pow(LL a, LL b) { LL ans = 1; while(b) { if(b&1) ans = ans*a%MOD; b>>=1; a = a*a%MOD; } return ans; } LL C(LL n, LL m) { if(m == 0)return 1; if(m > n)return 0; /* LL ans = 1; for(int i=1; i<=m; i++) { LL a = (n-m+i); LL b = i; ans = ans*(a*Pow(b, MOD-2)%MOD)%MOD; }这样会超时,所以要预处理阶乘; */ ///C(n, m) = n!/(m!(n-m)!)%MOD--->n!/m!%MOD * 1/((n-m)!)%MOD; /// a/b%MOD == a*b^(MOD-2)%MOD; LL ans = f[n]*Pow(f[m], MOD-2)%MOD * 1*Pow(f[n-m], MOD-2) % MOD; return ans; } void Fact(LL n) { f[0] = 1; for(int i=1; i<=n; i++) f[i] = f[i-1]*i%MOD; } int main() { Fact(MOD); int T, t = 1; LL n, k; scanf("%d", &T); while(T--) { scanf("%lld %lld", &n, &k); LL ans = C(n, k); printf("Case %d: %lld\n", t++, ans); } return 0; }
1067 - Combinations---LightOj(Lucas求组合数)
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/5746938.html