题解:P2155 [SDOI2008] 沙拉公主的困惑
MoonHalfxu · · 题解
题意:求
因为
又因为有
然后把
注意这里有个细节,如果
:::info[code]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e7+5;
int mod;
inline int fpow(ll a,int b){
ll res=1;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int jc[N];
int sm[N],ny[N];
bitset<N>vis;
signed main(){
int T,n=10000000,m;
cin>>T>>mod;
ll i,j;
jc[0]=sm[1]=ny[1]=1;
for(i=1;i<=n;i++){
jc[i]=1ll*jc[i-1]*i%mod;
if(i==mod)jc[i]=jc[i-1];
}
for(i=2;i<=n;i++){
sm[i]=sm[i-1];ny[i]=ny[i-1];
if(vis[i])continue;
sm[i]=1ll*sm[i]*(i-1)%mod;
if(i!=mod){
ny[i]=1ll*ny[i]*fpow(i,mod-2)%mod;
}
for(j=i*i;j<=n;j+=i){
vis[j]=1;
}
}
while(T--){
cin>>n>>m;
if(n>=mod && m<mod){
cout<<0<<"\n";
}
else{
// cout<<jc[n]<<" "<<ny[m]<<" "<<sm[m]<<"xxx\n";
cout<<1ll*jc[n]*ny[m]%mod*sm[m]%mod<<"\n";
}
}
return 0;
}
::::