結果

問題 No.2751 429-like Number
ユーザー naut3naut3
提出日時 2024-05-10 23:00:29
言語 Rust
(1.77.0)
結果
AC  
実行時間 361 ms / 4,000 ms
コード長 2,259 bytes
コンパイル時間 14,759 ms
コンパイル使用メモリ 401,948 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-10 23:00:51
合計ジャッジ時間 19,046 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 6 ms
6,944 KB
testcase_08 AC 17 ms
6,940 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 213 ms
6,944 KB
testcase_11 AC 358 ms
6,940 KB
testcase_12 AC 361 ms
6,940 KB
testcase_13 AC 18 ms
6,944 KB
testcase_14 AC 69 ms
6,944 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 76 ms
6,940 KB
testcase_17 AC 100 ms
6,940 KB
testcase_18 AC 190 ms
6,940 KB
testcase_19 AC 182 ms
6,940 KB
testcase_20 AC 198 ms
6,940 KB
testcase_21 AC 170 ms
6,944 KB
testcase_22 AC 177 ms
6,940 KB
testcase_23 AC 190 ms
6,940 KB
testcase_24 AC 190 ms
6,944 KB
testcase_25 AC 193 ms
6,944 KB
testcase_26 AC 187 ms
6,944 KB
testcase_27 AC 193 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![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
    }
}
0