結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
bal4u
|
| 提出日時 | 2019-05-18 18:36:05 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 237 ms / 9,973 ms |
| コード長 | 1,748 bytes |
| コンパイル時間 | 153 ms |
| コンパイル使用メモリ | 30,848 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:16:03 |
| 合計ジャッジ時間 | 1,473 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.c: In function 'in':
main.c:12:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
12 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:22:26: note: in expansion of macro 'gc'
22 | if ((c = gc()) < '0') break;
| ^~
main.c:13:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
13 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:23:25: note: in expansion of macro 'pc'
23 | if (ec) pc(c);
| ^~
ソースコード
// yukicoder 3030 ミラー・ラビン素数判定法のテスト
// 2019.5.18 bal4u
#include <stdio.h>
#include <stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
//// 高速入出力
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
ull in(int ec) { // 非負整数の入力
ull n = 0; int c;
while (1) {
if ((c = gc()) < '0') break;
if (ec) pc(c);
n = 10 * n + (c & 0xf);
}
return n;
}
//// ラビン素数テスト
#if 1
#define mulmod128(a,b,n) ((__int128_t)a*b % n)
#else
#define mod(a,m) ((a)%(m))
ull mulmod128(ull a, ull b, ull m) {
ull ans = 0;
a = mod(a, m), b = mod(b, m);
while (b > 0) {
if (b & 1) ans = mod(ans + a, m);
a = mod(a << 1, m);
b >>= 1;
}
return ans;
}
#endif
ull modpow(ull x, ull p, ull n) {
ull r = 1;
while (p > 0) {
if (p & 1) r = mulmod128(r, x, n);
x = mulmod128(x, x, n);
p >>= 1;
}
return r;
}
int ptbl[7] = { 2,325,9375,28178,450775,9780504,1795265022 };
int suspect(ull n) {
int i, j, b;
ull t, u, x;
u = n-1, t = 0; while ((u & 1) == 0) u >>= 1, t++;
for (j = 0; j < 7; j++) {
if ((b = ptbl[j]) >= n) {
b %= n;
if (b == 0) continue;
}
x = modpow(b, u, n);
if (x == 1 || x == n-1) continue;
i = 1; while (1) {
if (i++ == t) return 0;
x = mulmod128(x, x, n);
if (x == 1) return 0;
if (x == n-1) break;
}
}
return 1;
}
int miller_rabin(ull n) {
int p[16] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0 };
if (!(n >> 4)) return p[n];
if (!(n & 1)) return 0;
if (!(n % 5)) return 0;
return suspect(n);
}
int main()
{
int n = (int)in(0);
while (n--) {
ull x = in(1);
pc(' '), pc('0' + miller_rabin(x)), pc('\n');
}
return 0;
}
bal4u