結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
iwkjosec
|
| 提出日時 | 2019-06-02 16:50:53 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,385 bytes |
| コンパイル時間 | 10,799 ms |
| コンパイル使用メモリ | 401,296 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-18 16:42:27 |
| 合計ジャッジ時間 | 12,267 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 1 |
ソースコード
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let n: i32 = buf.trim().parse().unwrap();
for _ in 0..n {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let i: i64 = buf.trim().parse().unwrap();
println!("{} {}", i, if miller_rabin(i) { 1 } else { 0 });
}
}
const BASES64: [i128; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];
#[no_mangle]
pub extern "C" fn miller_rabin(n: i64) -> bool {
if n < 2 {
return false;
}
if (n & 1) == 0 {
return n == 2;
}
let mut d = n - 1;
let n = n as i128;
let mut s = 0;
while (d & 1) == 0 {
s += 1;
d >>= 1;
}
for a in BASES64.iter() {
if *a >= n {
break;
}
let mut c = true;
let mut x = mod_pow(*a, d, n);
if x == 1 {
c = false;
}
for _ in 0..s {
if x == n - 1 {
c = false;
}
x = x * x % n;
}
if c {
return false;
}
}
return true;
}
fn mod_pow(mut x: i128, mut n: i64, m: i128) -> i128 {
let mut res = 1i128;
while n != 0 {
if (n & 1) == 1 {
res = res * x % m;
}
x = x * x % m;
n >>= 1;
}
return res;
}
iwkjosec