费马小定理:
a*a^(p-2)%p=1,a的逆元即a^(p-2);
c(n,m)=n!/(n-m)!/m!=n!*(n-m)!^-1*m!^-1;
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
int fact[N],infact[N];//fact[i]为i的阶乘,infact[i]为i阶乘的逆元
int fpow(int a,int n,int p)
{
int ans=1;
while(n)
{
if(n&1) ans=ans*a%p;
a=a*a%p;
n>>=1;
}
return ans;
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);
int t,p; cin>>t>>p;
fact[0]=infact[0]=1;
for(int i=1;i<N;i++)
fact[i]=fact[i-1]*i%p;
for(int i=1;i<N;i++)
infact[i]=infact[i-1]*fpow(i,p-2,p)%p;
while(t--)
{
int n,m; cin>>n>>m;
cout<<fact[n]*infact[n-m]%p*infact[m]%p<<endl;
}
}
24/8/8