【SDOI2008】【BZOJ2186】【沙拉公主的困惑】【题解】【数论】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2186

题意:

求1-n!中与m!互质的个数,

这个讲得好http://blog.csdn.net/acdreamers/article/details/19068791

吐槽一下,一开始用数组bzoj爆CE,g++4.4说开不了这么大,用vectorTLE,再改回数组,A了……

Code:

/*
	ID:zky
	OJ:BZOJ
	Index:2186
	Language:C++
*/
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#define read(x) scanf("%lld",&x)
#define write(x) printf("%lld\n",x) 
using namespace std;
typedef long long lld;
const int N=10000005;
lld T,MOD;
lld fac[N];
lld prime1[N];
vector<bool>prime(N,1);
lld inv[N];
int main(){
	//fac.resize(N);
	//inv.resize(N);
	//prime1.resize(N);
	fac[1]=prime1[1]=1;
	fac[0]=prime1[0]=1;
	for(lld i=2;i<=10000000;i++){		
		if(prime[i]){
			for(lld j=i+i;j<=10000000;j+=i){
				prime[j]=false;
			}
		}
	}
	read(T);
	read(MOD); 
	for(lld i=2;i<=10000000;i++){
		fac[i]=i*fac[i-1]%MOD;
	}
	
	inv[1]=1;
	for(lld i=2;i<=10000000;i++){
		if(i>=MOD)break;
		inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
	}
	for(lld i=2;i<=10000000;i++){
		if(prime[i]){
			prime1[i]=prime1[i-1]*(i-1)%MOD;
			prime1[i]=prime1[i]*inv[i%MOD]%MOD;
		}else{
			prime1[i]=prime1[i-1];
		}
		}
	lld n,m;
	while(T--){
		read(n);read(m);
		write(fac[n]*prime1[m]%MOD);
	}
	return 0;
}


你可能感兴趣的:(数论,bzoj,逆元)