結果

問題 No.1237 EXP Multiple!
ユーザー あさくち
提出日時 2024-06-01 18:35:11
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 899 bytes
コンパイル時間 13,318 ms
コンパイル使用メモリ 379,784 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-12-22 05:40:57
合計ジャッジ時間 14,753 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

const MOD: usize = 1_000_000_007;

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

    let mut result = 1;

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

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

        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;
        }
    }

    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