QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91461#249. Miller Rabin 算法abcdeffa#WA 2ms3532kbC++201.9kb2023-03-28 21:57:262023-03-28 21:57:28

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-03-28 21:57:28]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 3532kb
  • [2023-03-28 21:57:26]
  • Submitted

answer

#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define LL long long
using namespace std;
LL read()
{
	LL X=0,w=0;char ch=0;
	while(!isdigit(ch))w|=ch=='-',ch=getchar();
	while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
	return w?-X:X;
}
LL nu[13]={2,3,5,7,11,13,17,19,23,29,31,37},p[10005];
LL times(LL x,LL y,LL mo)
{
	__int128 aa=x,bb=y;
	aa=aa*bb%mo;
	return (LL)aa;
}
LL ksm(LL x,LL y,LL mo)
{
	if(!y)return 1;
	LL z=ksm(x,y>>1,mo);
	z=times(z,z,mo);
	if(y&1)z=times(z,x,mo);
	return z;
}
bool Miller_Rabin(LL x)
{
	LL nuu=x-1,tt=0;
	fo(i,0,11)if(x==nu[i])return 1;
	while(!(nuu&1))nuu>>=1,tt++;
	fo(i,0,11)
	{
		LL aa=ksm(nu[i],nuu,x);
		if(aa==x-1||aa==1)continue;
		bool bz=0;
		fo(j,1,tt)
		{
			aa=times(aa,aa,x);
			if(aa==x-1)
			{
				bz=1;
				break;
			}
			if(aa==1)return 0;
		}
		if(!bz)return 0;
	}
	return 1;
}
LL gcd(LL x,LL y)
{
	if(!y)return x;
	return gcd(y,x%y); 
}
LL chg(LL x,LL y,LL mo)
{
	x=times(x,x,mo);
	return x+y-(x+y>=mo?mo:0);
}
LL Pollard_Rho(LL x)
{
	if(x%2==0)return 2;
	LL c=rand()*rand()%(x-1)+1,a1=0,a2=0,nw=1;
	for(int i=1;;i<<=1,a2=a1)
	{
		LL val=1;
		fo(j,1,i)
		{
			a1=chg(a1,c,x); 
			val=times(val,abs(a1-a2),x);
			if(j%128==0)
			{
				LL d=gcd(val,x);
				if(d>1)return d;
			}
		}
		if(i<128)
		{
	        LL d=gcd(val,x);
		    if(d>1)return d;
		}
	}
	return gcd(nw,x);
}
void fac(LL x)
{
	if(x==1)return;
	if(Miller_Rabin(x))
	{
		p[++p[0]]=x;
		return;
	}
	LL p1=Pollard_Rho(x);
	fac(p1);fac(x/p1);
}
int main()
{
	srand(time(0));
	freopen(".in","r",stdin);
	LL z;
	while(scanf("%lld",&z)!=EOF)
	{
		p[0]=0;
		if(z==1)
		{
			puts("N");
			continue;
		}
		if(Miller_Rabin(z))puts("Y");
		else puts("N");
		continue;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 3532kb

input:

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

output:


result:

wrong answer 1st lines differ - expected: 'N', found: ''