1 #include<cstdio>
 2 typedef __int64 LL;
 3 const int M=100010;
 4 class LUCAS { //lucas求组合数C(n,k)%p
 5     LL F[M];
 6     LL inv(LL a,LL mod) {
 7         if(a==1) return 1;
 8         return inv(mod%a,mod)*(mod-mod/a)%mod;
 9     }
10     void init(LL p) {
11         F[0]=1;
12         for(int i=1; i<=p; i++) {
13             F[i]=F[i-1]*i%p;
14         }
15     }
16     LL Lucas(LL n,LL k,LL p) {
17         LL ans=1;
18         while(n&&k) {
19             LL a=n%p;
20             LL b=k%p;
21             if(a<b) return 0;
22             ans=ans*F[a]%p*inv(F[b]*F[a-b]%p,p)%p;
23             n/=p;
24             k/=p;
25         }
26         return ans;
27     }
28 public:
29     LL solve(LL n,LL k,LL p){
30         init(p);
31         return Lucas(n,k,p);
32     }
33 }gx;
34 int main() {
35     int t,n,m,p;
36     while(~scanf("%d",&t)) {
37         while(t--) {
38             scanf("%d%d%d",&n,&m,&p);
39             printf("%d\n",(int)gx.solve(n+m,n,p));
40         }
41     }
42     return 0;
43 }
View Code

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-05-16
  • 2022-02-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
相关资源
相似解决方案