QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#126392#249. Miller Rabin 算法c20231020#TL 0ms0kbC++234.6kb2023-07-18 14:36:312023-07-18 14:36:33

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-18 14:36:33]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2023-07-18 14:36:31]
  • Submitted

answer

#define poj
#define zcz
#ifdef poj
//C++
#include<iomanip>
#include<iostream>
//C
#include<cassert>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
//STL
#include<algorithm>
#include<bitset>
#include<functional>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
//C++11
#if __cplusplus>=201103L
#include<chrono>
#include<random>
#include<unordered_set>
#include<unordered_map>
#endif
#else
#include<bits/stdc++.h>
#endif
using namespace std;
#ifdef zcz
class fastin{
	private:
#ifdef poj
	static const int MAXBF=1<<20;
#else
	const int MAXBF=1<<27;
#endif
	FILE *inf;
	char *inbuf,*inst,*ined;
	inline char _getchar(){
		if(inst==ined)inst=inbuf,ined=inbuf+fread(inbuf,1,MAXBF,inf);
		return inst==ined?EOF:*inst++;
	}
	public:
	fastin(FILE*_inf=stdin){
		inbuf=new char[MAXBF],inf=_inf,inst=inbuf,ined=inbuf;
	}
	~fastin(){delete inbuf;}
	template<typename Int> fastin&operator>>(Int &n){
		static char c;
		Int t=1;
		while((c=_getchar())<'0'||c>'9')if(c=='-')t=-1;
		n=(c^48);
		while((c=_getchar())>='0'&&c<='9')n=(n<<3)+(n<<1)+(c^48);
		n*=t;
		return *this;
	}
	fastin&operator>>(char*s){
		int t=0;
		static char c;
		while((c=_getchar())==' '||c=='\n');
		s[t++]=c;
		while((c=_getchar())!=' '&&c!='\n')s[t++]=c;
		s[t]='\0';
		return *this;
	}
}fi;
class fastout{
	private:
#ifdef poj
	static const int MAXBF=1<<20;
#else
	const int MAXBF=1<<27;
#endif
	FILE *ouf;
	char *oubuf,*oust,*oued;
	inline void _flush(){fwrite(oubuf,1,oued-oust,ouf);oued=oust;}
	inline void _putchar(char c){
		if(oued==oust+MAXBF)_flush(),oued=oubuf;
		*oued++=c;
		#ifdef local
		_flush();
		#endif
	}
	public:
	fastout(FILE*_ouf=stdout){
		oubuf=new char[MAXBF],ouf=_ouf,oust=oubuf,oued=oubuf;
	}
	~fastout(){_flush();delete oubuf;}
	template<typename Int> fastout&operator<<(Int n){
		if(n<0)_putchar('-'),n=-n;
		static char S[20];
		int t=0;
		do{S[t++]='0'+n%10,n/=10;}while(n);
		for(int i=0;i<t;++i)_putchar(S[t-i-1]);
		return *this;
	}
	fastout&operator<<(char c){_putchar(c);return *this;}
	fastout&operator<<(char*s){
		for(int i=0;s[i];++i)_putchar(s[i]);
		return *this;
	}
	fastout&operator<<(const char*s){
		for(int i=0;s[i];++i)_putchar(s[i]);
		return *this;
	}
}fo;
#define cin fi
#define cout fo
#else
#define czc ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#endif
#define mod 1000000007
#define ll long long
#define isinf 0x3f3f3f3f
#define ilinf 0x7fffffff
#define lsinf 0x3f3f3f3f3f3f3f3f
#define llinf 0x7fffffffffffffff
#define pii pair<int,int>
#define fr first
#define sc second
#define next ___
#define pb push_back
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rnd(1919810);
ll rng(ll l,ll r){return (rnd()%(r-l+1)+r-l+1)%(r-l+1)+l;}
ll mul(ll a,ll b,ll p){return (__int128)a*b%p;}
struct MR{
	ll pow(ll a,ll b,ll p){ll res=1;for(;b;b>>=1,a=mul(a,a,p))if(b&1)res=mul(res,a,p);return res;}
	ll mr(ll x,ll p){return pow(p,x-1,x)==1;}
	ll prime(ll x){
		if(x==2||x==3||x==5||x==13||x==19)return 1;
		if(x<2||(x!=2&&(!(x&1))))return 0;
//		ll T[]={2,3,5,7,11,13,17,19,23,29,31,37};			OK
//		ll T[]={2,3,5,7,11,13,17,19,61};					OK
//		ll T[]={3,5,7,11,13,37,79,97};						OK
		ll T[]={2,3,5,7,13,19,61,233};//					OK
//		for(ll i:t)if(i%x&&!mr(x,i))return 0;
		ll y=x-1,t,tt;
		while(!(y&1))y>>=1;
		t=y;
		for(int i=1;i<=8;++i){
			if(x==T[i-1])return 1;
			y=t;int f=0;
			tt=pow(T[i-1],y,x);
			if(tt==1)f=1;
			else for(;y&&y<x;tt=mul(tt,tt,x),y<<=1)if(tt==x-1){f=1;break;}
			if(!f)return 0;
		}
		return 1;
	}
//	inline bool prime(ll x){
//		if(x==46856248255981ll || x<2 )return false;
//		if(x==2 || x==3 || x==7 || x==61||x==101 || x==24251)return true;
//		return mr(x,2)&&mr(x,7)&&mr(x,101);
//	}
}mr;
ll mx;
struct PR{
	ll gcd(ll a,ll b){while(b)a^=b^=a^=b,b%=a;return a;}
	ll pr(ll x){
		ll a=0,b=0,c=rng(1,x),v=1;
		for(ll i=1;;i<<=1,a=b,v=1){
			for(ll j=1;j<=i;++j){
				b=(mul(b,b,x)+c)%x;
				v=mul(v,abs(b-a),x);
				if((j%127)==0){
					ll d=gcd(v,x);
					if(d>1)return d;
				}
			}
			ll d=gcd(v,x);
			if(d>1)return d;
		}
		return -1;
	}
	void fac(ll x){
		if(x<2||x<=mx)return;
		if(mr.prime(x)){mx=mx>x?mx:x;return;}
		ll p=x;
		while(p>=x)p=pr(x);
//		cout<<x<<' '<<p<<'\n';
		while(x%p==0)x/=p;
//		cout<<x<<' '<<p<<'\n';
		fac(x);fac(p);
	}
}pr;
void solve(){
	ll n;cin>>n;mx=0;
	if(mr.prime(n))cout<<"Y\n";
	else cout<<'N'<<'\n';
	return;
}
int main(){
	#ifndef zcz
	czc;
	#endif
	int t=1;cin>>t;
	while(t--)solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

996581938833575363
971646509461160317
773155992127361153
161603952726515268
540879144500456953
476831214764178553
784255927154201144
671096087619405061
805545190025269709
339546334309245137
337726347922962343
222956293307015293
809183111090275843
799050063298926344
691718101820598109
646220213113313...

output:


result: