QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91409#249. Miller Rabin 算法Minion#WA 11ms1876kbC++23980b2023-03-28 20:16:372023-03-28 20:16:39

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 20:16:39]
  • Judged
  • Verdict: WA
  • Time: 11ms
  • Memory: 1876kb
  • [2023-03-28 20:16:37]
  • Submitted

answer

#include<cstdio>
#include<algorithm>
#include<random>
#include<ctime>
#define fo(i,x,y) for(int i = x;i <= y;++i)
#define ll long long
#define p 998244353
#define mp 1000033
using namespace std;
int n;ll a;
ll f[70];
mt19937_64 gen(time(0));
inline ll mul(ll x,ll y,ll n)
{
	ll z = (x * y - (ll)(1.0L * x / n * y) * n);
	if(z >= n) z -= n;if(z < 0) z += n;return z;
}
ll ksm(ll x,ll y,ll n)
{
	ll res = 1;
	for(;y;y >>= 1,x = mul(x,x,n)) if(y & 1) res = mul(res,x,n);
	return res;
}
bool wit(ll a,ll n)
{
	ll u = n - 1,t = 0;
	while(u + 1 & 1) u >>= 1,++t;
	a = ksm(a,u,n);
	fo(i,1,t)
	{
		ll A = mul(a,a,n);
		if(A == 1)
		{
			if(a == 1 || a == n - 1) return 1;
			return 0;
		}
		a = A;
	}
	return a == 1;
}
bool mr(ll n)
{
	if(n < 2) return 0;
	if(n == 2) return 1;
	fo(i,1,20)
	{
		ll a = gen() % (n - 2) + 2;
		if(wit(a,n) == 0) return 0;
	}
	return 1;
}
int main() {while(scanf("%lld",&n) != EOF) puts(mr(n) ? "Y" : "N");}

详细

Test #1:

score: 0
Wrong Answer
time: 11ms
memory: 1876kb

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
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
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
...

result:

wrong answer 2nd lines differ - expected: 'Y', found: 'N'