結果

問題 No.1140 EXPotentiaLLL!
ユーザー kya_skikya_ski
提出日時 2020-08-01 00:01:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,634 ms / 2,000 ms
コード長 1,737 bytes
コンパイル時間 598 ms
コンパイル使用メモリ 65,548 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 03:46:50
合計ジャッジ時間 10,609 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,627 ms
4,380 KB
testcase_01 AC 173 ms
4,376 KB
testcase_02 AC 1,634 ms
4,376 KB
testcase_03 AC 276 ms
4,376 KB
testcase_04 AC 210 ms
4,376 KB
testcase_05 AC 337 ms
4,376 KB
testcase_06 AC 320 ms
4,376 KB
testcase_07 AC 1,559 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cstdint>

struct miller_rabin {
	using u128 = __uint128_t;
	using u64 = std::uint_fast64_t;
	using u32 = std::uint_fast32_t;
private :
	const std::array<u64, 7> a0 = {2, 3, 5, 7, 11, 13, 17};
	const std::array<u64, 7> a1 = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
	const u64 min_value = 341'550'071'728'321;

	void mul (u64 &x1, u64 x2, const u64 &mod) {
		u128 buff = x1;
		buff = (buff * x2) % mod;
		x1 = (u64)(buff);
	}

	u64 modpow (u64 x, u64 exp, const u64 &mod) {
		u64 cur = 1;
		while (exp) {
			if (exp & 1) mul(cur, x, mod);
			mul(x, x, mod); exp >>= 1;
		}
		return cur;
	}

	bool test (const u64 &a, const u64 &n, const u64 &d, const u32 &s) {
		u64 cur = modpow(a, d, n);
		if (cur == 1) return false;
		for (u32 r = 0; r < s; r++) {
			if (cur == n - 1) return false;
			mul(cur, cur, n);
		}
		return true;
	}

public :
	constexpr miller_rabin () = default;

	template<class T>
	bool operator() (const T &n) {
		if (n < 2) return false;
		if (n < 4) return true;
		if (not (n & 1)) return false;
		if (n < min_value) {
			u64 d = n - 1; u32 s = 0;
			while (not (d & 1)) { d >>= 1; s++; }
			for (const u64 &a : a0) {
				if (n <= a) return true;
				if (test(a, n, d, s)) return false;
			}
		} else {
			u64 d = n - 1; u32 s = 0;
			while (not (d & 1)) { d >>= 1; s++; }
			for (const u64 &a : a0) {
				if (n <= a) return true;
				if (test(a, n, d, s)) return false;
			}
		}
		return true;
	}

};


#include <iostream>

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	
	int t;
	std::cin >> t;
	
	miller_rabin prime;
	
	while (t--) {
		long long a;
		int p;
		std::cin >> a >> p;
		std::cout << (prime(p) ? (a % p != 0) : -1) << '\n';
	}
	
	return 0;
}
0