QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#505216 | #249. Miller Rabin 算法 | gogo11 | WA | 9ms | 3532kb | C++20 | 1.5kb | 2024-08-04 22:29:39 | 2024-08-04 22:29:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define lowbit(x) ((x) & (-x))
constexpr int N = 2e6 + 10;
constexpr int M = 2e5 + 10;
constexpr ll mod = 1e9+7;
constexpr double eps = 1e-9;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LNF = 0x3f3f3f3f3f3f3f3f;
constexpr ll Base = 100129;
constexpr int MAXBIT = 30;
ll power(ll r1,ll r2,ll r3)
{
ll res=1;
while(r2)
{
if(r2&1)
res*=r1,res%=r3;
r1*=r1,r1%=r3;
r2>>=1;
}
return res;
}
bool MillerRabin(ll n)
{
if(n==2) return true;
if(n<=1 || n%2==0) return false;
ll base[7]={2,325,9375,28178,450775,9780504,1795265022};
ll u=n-1,k=0;
while(u%2==0) u/=2,k++;
for(auto x : base)
{
if(x%n==0) continue;
ll v=power(x,u,n);
if(v==1 || v==n-1) continue;
for(int j=1;j<=k;j++)
{
ll last=v;
v=(__int128_t)v*v%n;
if(v==1)
{
if(last!=n-1) return false;
break;
}
}
if(v!=1) return false;
}
return true;
}
void solve()
{
ll x;
while(cin>>x)
{
if(MillerRabin(x))
cout<<"Y"<<"\n";
else
cout<<"N"<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _=1;
// cin>>_;
while(_--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 9ms
memory: 3532kb
input:
996581938833575363 971646509461160317 773155992127361153 161603952726515268 540879144500456953 476831214764178553 784255927154201144 671096087619405061 805545190025269709 339546334309245137 337726347922962343 222956293307015293 809183111090275843 799050063298926344 691718101820598109 646220213113313...
output:
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 N ...
result:
wrong answer 2nd lines differ - expected: 'Y', found: 'N'