結果

問題 No.1237 EXP Multiple!
ユーザー あさくち
提出日時 2024-06-01 18:55:37
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 959 bytes
コンパイル時間 11,867 ms
コンパイル使用メモリ 378,032 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-12-22 06:03:36
合計ジャッジ時間 13,220 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

const MOD: usize = 1_000_000_007;

fn main() {
    input! {
        n: usize,
        a: [usize; n],
    }

    if a.contains(&0) {
        println!("-1");
        return;
    }

    let mut result = 1;

    for i in 0..n {
        let x = a[i];

        if x == 1 {
            continue;
        }

        if let Some(fac_x) = factorial(x) {
            result *= x.pow(fac_x as u32);

            if result > MOD {
                println!("{}", MOD);
                return;
            }
        } else {
            println!("{}", MOD);
            return;
        }
    }

    if result == 0 {
        println!("-1");
        return;
    }

    println!("{}", MOD % result);
}

fn factorial(n: usize) -> Option<usize> {
    if n == 0 {
        return Some(1);
    }

    let mut result = 1;

    for i in (1..=n).rev() {
        result *= i;

        if result > MOD {
            return None;
        }
    }

    Some(result)
}
0