QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#91608 | #249. Miller Rabin 算法 | youngsystem# | WA | 59ms | 3476kb | C++ | 1.2kb | 2023-03-29 10:03:25 | 2023-03-29 10:03:28 |
Judging History
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'