QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#31939#249. Miller Rabin 算法whereismyteammate#WA 161ms3828kbC++20964b2022-05-13 23:24:312022-05-13 23:24:33

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-13 23:24:33]
  • 评测
  • 测评结果:WA
  • 用时:161ms
  • 内存:3828kb
  • [2022-05-13 23:24:31]
  • 提交

answer

#include <bits/stdc++.h>

std::mt19937 s(std::chrono::steady_clock::now().time_since_epoch().count());
int64_t mul(int64_t x, int64_t y, int64_t mod) {
	return (__int128) x * y % mod;	
}
int64_t fastpow(int64_t x, int64_t p, int64_t mod) {
	int64_t r = 1;
	while (p) {
		if (p & 1) r = mul(r, x, mod);
		x = mul(x, x, mod);
		p >>= 1;
	}
	return r;
}

bool test(int64_t n) {
	if (n == 1) return false;
	if (n == 2 || n == 3) return true;
	if (n % 2 == 0) return false;
	int64_t a = n - 1, b = 0;
	while (a % 2 == 0) a >>= 1, ++b;
	for (int i = 0; i < 4; ++i) {
		int64_t x = s() % (n - 2) + 2;
		int64_t v = fastpow(x, a, n);
		if (v == 1) continue;
		bool ok = false;
		for (int j = 0; j < b; ++j) {
			if (v == n - 1) {
				ok = true;
				break;
			}
			v = mul(v, v, n);
		}
		if (!ok) return false;
	}
	return true;
}

int main() {
	int64_t n;
	while (scanf("%lld", &n) == 1)  {
		printf("%c\n", "NY"[test(n)]);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 161ms
memory: 3732kb

input:

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

output:

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

result:

ok 15000 lines

Test #2:

score: 0
Accepted
time: 107ms
memory: 3824kb

input:

292094793288448159
456918231632780153
52701684901220791
755430520029564023
202037556396478813
224321375550698537
953758266735232253
68668310674613297
730895589897264853
344428227893888993
521429852982590257
547788290718839273
332181270020381261
876010276438312333
906474115171090099
26200832832269134...

output:

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

result:

ok 15000 lines

Test #3:

score: -100
Wrong Answer
time: 49ms
memory: 3828kb

input:

37686301288201
83818601792630641
73103085605161
146313732835525609
228087610722516540
343255869017446321
132173451449926849
461798530935794823
171613522570639321
746139393134794441
700705956080852569
186402586980996590
116687280644971921
439801455648601
608187424599317161
582838391995869241
29815648...

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 4278th lines differ - expected: 'N', found: 'Y'