結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
Nachia
|
| 提出日時 | 2022-08-31 20:04:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 43 ms / 9,973 ms |
| コード長 | 1,184 bytes |
| コンパイル時間 | 134 ms |
| コンパイル使用メモリ | 30,080 KB |
| 最終ジャッジ日時 | 2025-02-07 00:14:14 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:34:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
34 | int T; scanf("%d", &T);
| ~~~~~^~~~~~~~~~
main.cpp:36:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
36 | unsigned long long N; scanf("%llu", &N);
| ~~~~~^~~~~~~~~~~~
ソースコード
#include <initializer_list>
bool IsPrime(unsigned long long x) noexcept {
if(x <= 1) return false;
if(x % 2 == 0) return x == 2;
using u64 = unsigned long long;
using u128 = __uint128_t;
u64 d = x-1;
int s = 0;
int q = 63;
while(!(d&1)){ d >>= 1; s++; }
while(!(d >> q)) q--;
u64 r = x; for(int t=0; t<5; t++) r*=2-r*x;
u64 n2 = -(u128)x % x;
auto red = [=](u128 t) noexcept -> u64 {
t = (t + (u128)((u64)t*-r)*x) >> 64;
return (t >= x) ? t-x : t;
};
u64 one = red(n2);
for(u64 base : { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }){
if(base%x==0) continue;
u64 a = base = red((u128)(base%x)*n2);
for(int e=q-1; e>=0; e--){ a = red((u128)a*a); if((d>>e)&1) a = red((u128)a*base); }
if(a == one) continue;
for(int t=1; t<s&&a!=x-one; t++) a = red((u128)a*a);
if(a != x-one) return false;
}
return true;
}
#include <cstdio>
int main(){
int T; scanf("%d", &T);
for(int i=0; i<T; i++){
unsigned long long N; scanf("%llu", &N);
bool ans = IsPrime(N);
printf("%llu %s", N, ans ? "1\n" : "0\n");
}
return 0;
}
Nachia