QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91733#249. Miller Rabin 算法zghtyarecrenj#TL 0ms0kbC++141.7kb2023-03-29 14:51:222023-03-29 14:51:26

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-29 14:51:26]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-03-29 14:51:22]
  • 提交

answer

#include <bits/stdc++.h>

typedef unsigned long long u64;

const int N = 5;

u64 n;

inline u64 addint(u64 x) { return x < n ? x : x - n; }
inline u64 mul(u64 x, u64 y) { return ((__uint128_t)x * y) % n; }
inline void Add(u64 &x, u64 y) { x += y; if (x >= n) x -= n; }

struct mat { u64 a[N][N]; };

mat *I, *P, *Q;

inline void MatMul(mat *A, mat *B, mat *C) {
	for (int i = 0; i < N; ++i)
		for (int j = i; j < N; ++j) {
			u64 t = 0;
			for (int k = 0; k < N; ++k) Add(t, mul(A->a[i][k], B->a[k][j]));
			C->a[i][j] = t;
			if (i != j) C->a[j][i] = C->a[i][j];
		}
}

inline mat QuickPow(mat *R, u64 n) {
	mat mre = *R, mbb = *I, mt = *I;
	mat *re = &mre, *bb = &mbb, *t = &mt;
	while (n > 0) {
		if (n & 1) {
			MatMul(bb, re, t);
			std::swap(t, bb);
		}
		n >>= 1;
		MatMul(re, re, t);
		std::swap(t, re);
	}
	return *bb;
}

bool isP(mat *x, mat *y) {
	for (int i = 0; i < N; ++i)
		if (x->a[i][i] != addint(y->a[i][i] + 1)) return 0;
	for (int i = 0; i < N; ++i)
		for (int j = i + 1; j < N; ++j)
			if (x->a[i][j] != y->a[i][j]) return 0;
	return 1;
}

void init_mat() {
	I = new mat({1, 0, 0, 0, 0,
				 0, 1, 0, 0, 0,
				 0, 0, 1, 0, 0,
				 0, 0, 0, 1, 0, 
				 0, 0, 0, 0, 1});
	P = new mat({	1,	2,	5,	11,	37,
				 	2,	1,	17,	31,	13,
				 	5,	17,	1,	23,	7,
				 	11,	31,	23,	1,	3,
				 	37,	13,	7,	3,	1	});
	Q = new mat({	0,	2,	5,	11,	37,
				 	2,	0,	17,	31,	13,
				 	5,	17,	0,	23,	7,
				 	11,	31,	23,	0,	3,
				 	37,	13,	7,	3,	0	});
}

int main() {
	init_mat();
	while (scanf("%llu", &n) != EOF) {
		if (n == 1) { puts("N"); continue; }
		mat PP = QuickPow(P, n), QQ = QuickPow(Q, n);
		puts(isP(&PP, &QQ) ? "Y" : "N");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

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: