1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 typedef long long LL; 7 8 LL n,m,p; 9 10 LL quick_mod(LL a, LL b) 11 { 12 LL ans = 1; 13 a %= p; 14 while(b) 15 { 16 if(b & 1) 17 { 18 ans = ans * a % p; 19 b--; 20 } 21 b >>= 1; 22 a = a * a % p; 23 } 24 return ans; 25 } 26 27 LL C(LL n, LL m) 28 { 29 if(m > n) return 0; 30 LL ans = 1; 31 for(int i=1; i<=m; i++) 32 { 33 LL a = (n + i - m) % p; 34 LL b = i % p; 35 ans = ans * (a * quick_mod(b, p-2) % p) % p; 36 } 37 return ans; 38 } 39 40 LL Lucas(LL n, LL m) 41 { 42 if(m == 0) return 1; 43 return C(n % p, m % p) * Lucas(n / p, m / p) % p; 44 } 45 46 int main() 47 { 48 int T; 49 scanf("%d", &T); 50 while(T--) 51 { 52 scanf("%I64d%I64d%I64d", &n, &m, &p); 53 printf("%I64d\n", Lucas(n,m)); 54 } 55 return 0; 56 }
时间: 2024-11-18 22:43:45