結果
| 問題 |
No.2785 四乗足す四の末尾の0
|
| コンテスト | |
| ユーザー |
naut3
|
| 提出日時 | 2024-06-14 23:12:35 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 191 ms / 2,000 ms |
| コード長 | 1,827 bytes |
| コンパイル時間 | 11,945 ms |
| コンパイル使用メモリ | 378,900 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-14 23:12:51 |
| 合計ジャッジ時間 | 14,460 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
#![allow(non_snake_case, unused_imports)]
use proconio::{fastout, input, marker::*};
#[fastout]
fn main() {
input! {
T: usize,
}
for _ in 0..T {
input! {
N: i128,
}
let N = N * N * N * N + 4;
let mut N = N as u128;
if miller_rabin_primality_test(N) {
println!("Yes\n0");
} else {
let mut cnt = 0;
while N % 10 == 0 {
N /= 10;
cnt += 1;
}
println!("No\n{}", cnt);
}
}
}
/// 64bit整数じゃないからヤバそう
pub fn miller_rabin_primality_test(n: u128) -> bool {
fn pow(a: u128, b: u128, m: u128) -> u128 {
let (mut a, mut b, m) = (a as u128, b as u128, m as u128);
let mut ret = 1;
a %= m;
while b > 0 {
if b & 1 == 1 {
ret = (ret * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
ret as u128
}
if n == 0 || n == 1 {
false
} else if n == 2 {
true
} else if n % 2 == 0 {
false
} else {
const A: [u128; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];
let mut s = 0;
let mut d = n - 1;
while d % 2 == 0 {
s += 1;
d >>= 1;
}
for a in A {
if a % n == 0 {
return true;
}
let mut x = pow(a, d, n);
if x != 1 {
for t in 0..s {
if x == n - 1 {
break;
}
x = pow(x, 2, n);
if t == s - 1 {
return false;
}
}
}
}
true
}
}
naut3