QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91608#249. Miller Rabin 算法youngsystem#WA 59ms3476kbC++1.2kb2023-03-29 10:03:252023-03-29 10:03: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-29 10:03:28]
  • Judged
  • Verdict: WA
  • Time: 59ms
  • Memory: 3476kb
  • [2023-03-29 10:03:25]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
int ksm(int x,int k,int p)
{
	int ans=1;
	while(k>=1)
	{
		if(k&1)ans=(__int128)ans*x%p;
		x=(__int128)x*x%p;
		k>>=1;
	}
	return ans;
}
bool check(int p,int x)
{
	int r=0,d=x-1;
	while(d%2==0)d/=2,r++;
	int now=ksm(p,d,x),pre=0;
	for(int i=1;i<=r;i++)
	{
		pre=now;
		now=(__int128)now*now%x;
		if(now==1)
		{
			if(pre==1||pre==x-1)return true;
			return false;
		}
	}
	return false;
}
int jc[12]={2,3,5,7,11,13,17,19,61,2333,4567,24251};
signed main()
{
	int x;
	while(cin>>x)
	{
		if(x<=1)
		{
			printf("N\n");
			continue;
		}
		bool flag=false;
		for(int i=0;i<12;i++)
		{
			if(x==jc[i])
			{
				flag=true;
				printf("Y\n");
				break;
			}
			if(x%jc[i]==0)
			{
				flag=true;
				printf("N\n");
				break;
			}
		}
		if(flag==true)continue;
		flag=true;
		for(int i=0;i<12;i++)
		{
			if(check(jc[i],x))
			{
				flag=false;
				printf("N\n");
				break;
			}
		}
		if(flag==true)printf("Y\n");
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 59ms
memory: 3476kb

input:

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

output:

Y
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
...

result:

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