fork(5) download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll fact[1000000];
  5. int pwr(int x,int p,int mod){
  6. ll t = 1,a=x;
  7. while(p){
  8. //condition need to be added for case of odd
  9. if(p&1) t = (t*a)%mod;
  10. a=(a*a)%mod;
  11. p=p>>1;
  12. }
  13. return t;
  14. }
  15. ll abc(int n,int r,int MOD){
  16. ll tem=(fact[r]*fact[n-r])%MOD;
  17. //by fermat`s theorem
  18. //the inverse is found by (x^(p-2))%p
  19. tem=pwr(tem,MOD-2,MOD);
  20. return (tem*fact[n])%MOD;
  21. }
  22. ll xyz(ll n,ll m,int p){
  23. //changed if condition
  24. if (n==0 && m==0) return 1;
  25. int ni=n%p;
  26. int mi=m%p;
  27. return xyz(n/p,m/p,p)*abc(ni,mi,p)%p;
  28. }
  29. ll C(ll n,ll r,int MOD){
  30. fact[0]=1;
  31. for(int i=1;i<MOD;i++) fact[i]=(i*fact[i-1])%MOD;
  32. return xyz(n,r,MOD);
  33. }
  34. int main(){
  35. int t;
  36. cin>>t;
  37. while(t--){
  38. ll n,k;
  39. int p;
  40. cin>>n>>k>>p;
  41. cout<<C(n,k,p)<<endl;
  42. }
  43. }
Success #stdin #stdout 0s 11280KB
stdin
2
10 2 7
4 2 3 
stdout
3
0