1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <cstring> #include <cstdio> using namespace std; typedef long long LL; LL n,m,p; LL ksm(LL a,LL k){ LL res = 1; for (;k;k>>=1,a=a*a%p) if (k&1) res = res*a%p; return res; } LL C(LL n,LL m){ if(m>n)return 0; LL ans=1; for(int i=1;i<=m;i++){ LL a=(n+i-m)%p; LL b=i%p; ans=ans*(a*ksm(b,p-2)%p)%p; } return ans; } LL Lucas(LL n,LL m){ if(m==0)return 1; return C(n%p,m%p)*Lucas(n/p,m/p)%p; } int main(){ scanf("%d",&T); scanf("%lld%lld%lld",&n,&m,&p); printf("%lld\n",Lucas(n,m)); }
|