朴素实现的代码
//exlucas
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
_Pragma("GCC diagnostic ignored \"-Wconversion\"")
const int __RSIZ=2e7;
const int __WSIZ=4e6;
class fastio{//快读快写
private:
char buf[(int)__RSIZ],*p1;
char pbuf[(int)__WSIZ],*p2;
char gc(){return *(p1++);}
void pc(char c){*(p2++)=c;}
template<typename tp>
void _wr(tp x){
if(!x)return;
_wr(x/10);
pc((x%10)|48);
}
public:
fastio(){
#ifndef ONLINE_JUDGE
cerr<<"R:"<<double(sizeof buf)/1024.0/1024.0<<endl;
cerr<<"W:"<<double(sizeof pbuf)/1024.0/1024.0<<endl;
#endif
fread(buf,1,sizeof buf,stdin);
p1=buf,p2=pbuf;
}
~fastio(){fwrite(pbuf,1,p2-pbuf,stdout);}
char getchar(){
while(*p1<32||*p1>126) p1++;
return *p1;
}
template<typename tp>
void read(tp &x){
x=0;tp f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
x*=f;
}
void write(char x){
pc(x);
}
template<typename tp>
void write(tp x){
if(x<0) pc('-'),_wr(-x);
else if(x==0)pc('0');
else _wr(x);
}
template<typename tp>
fastio& operator<<(tp x){
write(x);
return *this;
}
template<typename tp>
fastio& operator>>(tp &x){
read(x);
return *this;
}
};
fastio io;
#define rd(x) io.read(x)
#define wr(x) io.write(x)
#define cin io
#define cout io
#define endl '\n'
_Pragma("GCC diagnostic ignored \"-Wconversion\"")
bool _a;
ll mod;
map<ll,ll> pd;//质因数分解 -> 数组下标的映射
ll cnt[N];//质因子的指数
ll tot;//质因数个数
ll num[N];//p^k,即每一步的模数
ll *f[N];//预处理的连乘积
void break_down(ll x){//质因数分解
for(ll i=2;i*i<=x;i++){//枚举到 根号
if(x%i==0){//i 是 x 的质因子
tot++;
pd[i]=tot;
while(x%i==0){//算 x 的出现次数
cnt[tot]++;
x/=i;
}
}
}
if(x>1){
if(!pd[x]) pd[x]=++tot;
cnt[pd[x]]++;
}
}
ll fpow(ll x,ll p,ll P){ //快速幂
ll res=1;
while(p){
if(p&1) res=(res*x)%P;
x=(x*x)%P;
p>>=1;
}
return res;
}
ll exgcd(ll a,ll b,ll &x,ll &y){ //扩展欧几里得算法
if(b==0){x=1,y=0;return a;}
ll d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
ll getinv(ll a,ll P){//求逆元
ll x,y;
exgcd(a,P,x,y);
return (x%P+P)%P;
}
void init(){
break_down(mod);//先把模数质因数分解
for(auto &y:pd){//枚举质因子
ll p=y.first,r=y.second;
num[r]=fpow(p,cnt[r],(ll)1e9);//求 p^k ,模数取 1e9 大于 p^k, 没有影响
f[r]=new ll[num[r]+1];//f[pos][i] i! 在模 p_i^k_i 的意义下与 p_i 互质部分的结果
f[r][0]=1;
for(ll i=1;i<=num[r];i++){
if(i%p==0){//该项是 p 的倍数,不管(否则答案都是0)
f[r][i]=f[r][i-1];
}else{//该项与 p 互质(因为 p 是质数)
f[r][i]=f[r][i-1]*i%num[r];
}
}
}
}
ll alpha(ll x,ll p){//求 x! 中 p 的指数
if(x<p) return 0;
return alpha(x/p,p)+x/p;
}
ll G(ll x,ll p){
//求 x! 中与 p 互质的部分 模 p^k 的结果
if(x==0) return 1;
ll pos=pd[p];
ll nm=num[pos];
ll res=1;
res=fpow(f[pos][nm],x/nm,nm);//上面的第一项
res=res*G(x/p,p)%nm;//上面式子的第三项
res=res*f[pos][x%nm]%nm;//上面式子的第二项
return res;
}
ll getC(ll n,ll m,ll p){//求 C(n,m) %P;
ll pos=pd[p];
ll k=cnt[pos],nm=num[pos];
ll t=alpha(n,p)-alpha(m,p)-alpha(n-m,p);
if(t>=k) return 0;//一个小优化:有超过 k 个 p 因子,这一项取模后一定为 0
ll res=fpow(p,t,nm);
res=res*G(n,p)%nm;
res=res*getinv(G(m,p),nm)%nm;
res=res*getinv(G(n-m,p),nm)%nm;
return res;
}
ll crt(ll a[],ll b[],ll t){//中国剩余定理
ll M=0,P=1;
for(ll i=1;i<=t;i++) P*=b[i];
for(ll i=1;i<=t;i++){
M+=a[i]*(P/b[i])%P*getinv(P/b[i],b[i])%P;
M%=P;
}
return M;
}
ll a[N];
ll exlucas(ll n,ll m){
for(auto y:pd){
ll p=y.first;
ll pos=y.second;
a[pos]=getC(n,m,p);
}
return crt(a,num,tot);
}
bool _b;
int main(){
ll n,m,T;
// cin>>T>>mod;//code for loj #181
cin>>n>>m;//code for 洛谷 P4720
init();
// while(T--)(io>>n>>m)<<exlucas(n,m)<<endl; //code for loj #181
cout<<exlucase(n,m);//code for 洛谷 P4720
return 0;
}优化后的实现:
//exlucas
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
_Pragma("GCC diagnostic ignored \"-Wconversion\"")
const int __RSIZ=2e7;
const int __WSIZ=4e6;
class fastio{//快读快写
private:
char buf[(int)__RSIZ],*p1;
char pbuf[(int)__WSIZ],*p2;
char gc(){return *(p1++);}
void pc(char c){*(p2++)=c;}
template<typename tp>
void _wr(tp x){
if(!x)return;
_wr(x/10);
pc((x%10)|48);
}
public:
fastio(){
// freopen("evil.in","r",stdin);
// freopen("out.txt","w",stdout);
#ifndef ONLINE_JUDGE
cerr<<"R:"<<double(sizeof buf)/1024.0/1024.0<<endl;
cerr<<"W:"<<double(sizeof pbuf)/1024.0/1024.0<<endl;
#endif
fread(buf,1,sizeof buf,stdin);
p1=buf,p2=pbuf;
}
~fastio(){fwrite(pbuf,1,p2-pbuf,stdout);}
char getchar(){
while(*p1<32||*p1>126) p1++;
return *p1;
}
template<typename tp>
void read(tp &x){
x=0;tp f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
x*=f;
}
void write(char x){
pc(x);
}
template<typename tp>
void write(tp x){
if(x<0) pc('-'),_wr(-x);
else if(x==0)pc('0');
else _wr(x);
}
template<typename tp>
fastio& operator<<(tp x){
write(x);
return *this;
}
template<typename tp>
fastio& operator>>(tp &x){
read(x);
return *this;
}
};
fastio io;
#define rd(x) io.read(x)
#define wr(x) io.write(x)
#define cin io
#define cout io
#define endl '\n'
_Pragma("GCC diagnostic ignored \"-Wconversion\"")
bool _a;
ll mod;
template<typename tp>
struct myhash{
using ull=unsigned long long;
ull mask;
myhash(){
mt19937_64 rnd(time(0));
mask=rnd();
}
size_t xor_shift(ull x) const{
x^=mask;
x^=x<<7;
x^=x>>13;
x^=x<<54;
x^=mask;
return x;
}
size_t operator() (tp x) const{
return xor_shift((ull)x);
}
};
__gnu_pbds::gp_hash_table<ll,ll,myhash<ll> > pd;//质因数分解 -> 数组下标的映射 (现在必须用哈希表了,否则map成复杂度瓶颈了)
ll cnt[N];ll tot;
ll num[N];
ll *f[N];
ll phis[N];
ll *pw[N];
void break_down(ll x){//质因数分解
for(ll i=2;i*i<=x;i++){//枚举到根号
if(x%i==0){//i 是 x 的质因子
tot++;
pd[i]=tot;
while(x%i==0){//算 x 的出现次数
cnt[tot]++;
x/=i;
}
}
}
if(x>1){
if(!pd[x]) pd[x]=++tot;
cnt[pd[x]]++;
}
}
ll get_phi(ll x){
ll phi=x;
for(ll i=2;i*i<=x;i++){
if(x%i==0){
phi=phi/i*(i-1);
while(x%i==0)x/=i;
}
}
if(x>1) phi=phi/x*(x-1);
return phi;
}
ll fpow(ll x,ll p,ll P){ //快速幂
ll res=1;
while(p){
if(p&1) res=(res*x)%P;
x=(x*x)%P;
p>>=1;
}
return res;
}
ll exgcd(ll a,ll b,ll &x,ll &y){ //扩展欧几里得算法
if(b==0){x=1,y=0;return a;}
ll d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
ll getinv(ll a,ll P){//求逆元
ll x,y;
exgcd(a,P,x,y);
return (x%P+P)%P;
}
void init(){
break_down(mod);//先把模数质因数分解
for(auto &y:pd){//枚举质因子
ll p=y.first,r=y.second;
num[r]=fpow(p,cnt[r],(ll)1e9);//求 p^k ,模数取 1e9 大于 p^k, 没有影响
f[r]=new ll[num[r]+1];//f[pos][i] i! 在模 p_i^k_i 的意义下与 p_i 互质部分的结果
f[r][0]=1;
phis[r]=get_phi(num[r]);
pw[r]=new ll[2*phis[r]];
for(ll i=1;i<=num[r];i++){
if(i%p==0){//该项是 p 的倍数,不管(否则答案都是0)
f[r][i]=f[r][i-1];
}else{//该项与 p 互质(因为 p 是质数)
f[r][i]=f[r][i-1]*i%num[r];
}
}
pw[r][0]=1;
for(ll i=1;i<2*phis[r];i++){
pw[r][i]=pw[r][i-1]*f[r][num[r]]%num[r];
}
}
}
ll alpha(ll x,ll p){//求 x! 中 p 的指数
if(x<p) return 0;
return alpha(x/p,p)+x/p;
}
ll G(ll x,ll p){
// cerr<<"G:"<<x<<" "<<p<<endl;
//求 x! 中与 p 互质的部分 模 p^k 的结果
if(x==0) return 1;
ll pos=pd[p];
ll nm=num[pos];
ll res=1;
// res=fpow(f[pos][nm],x/nm,nm);
if(x/nm<phis[pos]) res=pw[pos][x/nm];
else res=pw[pos][(x/nm)%phis[pos]+phis[pos]];
res=res*G(x/p,p)%nm;
res=res*f[pos][x%nm]%nm;
return res;
}
ll getC(ll n,ll m,ll p){//求 C(n,m) %p^k;
ll pos=pd[p];
ll k=cnt[pos],nm=num[pos];
ll t=alpha(n,p)-alpha(m,p)-alpha(n-m,p);
if(t>=k) return 0;
ll res=fpow(p,t,nm);
res=res*G(n,p)%nm;
res=res*getinv(G(m,p),nm)%nm;
res=res*getinv(G(n-m,p),nm)%nm;
return res;
}
ll crt(ll a[],ll b[],ll t){
ll M=0,P=1;
for(ll i=1;i<=t;i++) P*=b[i];
for(ll i=1;i<=t;i++){
M+=a[i]*(P/b[i])%P*getinv(P/b[i],b[i])%P;
M%=P;
}
return M;
}
ll a[N];
ll exlucas(ll n,ll m){
for(auto y:pd){
ll p=y.first;
ll pos=y.second;
a[pos]=getC(n,m,p);
}
return crt(a,num,tot);
}
bool _b;
int main(){
ll n,m,T;
cin>>T>>mod;//code for loj #181
// cin>>n>>m;//code for 洛谷 P4720
init();
while(T--)(io>>n>>m)<<exlucas(n,m)<<endl; //code for loj #181
// cout<<exlucas(n,m);//code for 洛谷 P4720
return 0;
}