結果
| 問題 |
No.2751 429-like Number
|
| コンテスト | |
| ユーザー |
naut3
|
| 提出日時 | 2024-05-10 23:00:29 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 350 ms / 4,000 ms |
| コード長 | 2,259 bytes |
| コンパイル時間 | 12,583 ms |
| コンパイル使用メモリ | 401,752 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-20 07:09:43 |
| 合計ジャッジ時間 | 16,946 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 22 |
ソースコード
#![allow(non_snake_case, unused_imports)]
use proconio::{fastout, input, marker::*};
#[fastout]
fn main() {
input! {
Q: usize,
A: [usize; Q],
}
for mut a in A {
let mut cnt = 0;
for i in 2.. {
if i * i * i > a {
break;
}
if a % i == 0 {
while a % i == 0 {
cnt += 1;
a /= i;
}
break;
}
}
if cnt == 0 {
println!("No");
continue;
}
for i in 2.. {
if i * i > a {
break;
}
if a % i == 0 {
while a % i == 0 {
cnt += 1;
a /= i;
}
break;
}
}
if (cnt == 2 && miller_rabin_primality_test(a)) || (cnt == 3 && a == 1) {
println!("Yes");
} else {
println!("No");
}
}
}
pub fn miller_rabin_primality_test(n: usize) -> bool {
fn pow(a: usize, b: usize, m: usize) -> usize {
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 usize
}
if n == 0 || n == 1 {
false
} else if n == 2 {
true
} else if n % 2 == 0 {
false
} else {
const A: [usize; 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