标签:des style blog http io color ar os sp
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 typedef long long LL; 7 8 const int MAXN = 100010; 9 10 int fac[MAXN]; 11 int n, m, p, T; 12 13 int power(int x, int p, int mod) { 14 int res = 1; 15 while(p) { 16 if(p & 1) res = (LL)res * x % mod; 17 x = (LL)x * x % mod; 18 p >>= 1; 19 } 20 return res; 21 } 22 23 void init_fac(int p) { 24 fac[0] = 1; 25 for(int i = 1; i <= p; ++i) 26 fac[i] = (LL)fac[i - 1] * i % p; 27 } 28 29 int lucas(int n, int m, int p) { 30 int res = 1; 31 while(n && m) { 32 int a = n % p, b = m % p; 33 if(a < b) return 0; 34 res = (LL)res * fac[a] * power((LL)fac[b] * fac[a - b] % p, p - 2, p) % p;//三次乘法注意 35 n /= p; 36 m /= p; 37 } 38 return res; 39 } 40 41 int main() { 42 scanf("%d", &T); 43 while(T--) { 44 scanf("%d%d%d", &n, &m, &p); 45 init_fac(p); 46 printf("%d\n", lucas(n + m, m, p)); 47 } 48 }
HDU 3037 Saving Beans(Lucas定理模板题)
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/oyking/p/4101532.html