結果

問題 No.1084 積の積
ユーザー phsplsphspls
提出日時 2023-01-04 20:22:48
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 12,128 ms
コンパイル使用メモリ 378,728 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-28 00:08:17
合計ジャッジ時間 14,638 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

const MOD: usize = 1e9 as usize + 7;
const LIMIT: usize = 1e9 as usize;

fn power(base: usize, times: usize) -> usize {
    if times == 0 { return 1usize; }
    if times == 1 { return base; }
    let temp = power(base, times/2);
    temp * temp % MOD * power(base, times%2) % MOD
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    if a.iter().filter(|&&v| v == 0).count() > 0 {
        println!("0");
        return;
    }
    let mut ridx = 0usize;
    let mut current = 1usize;
    let mut summary = 1usize;
    while ridx < n && current * a[ridx] < LIMIT {
        current *= a[ridx];
        summary = summary * current % MOD;
        ridx += 1;
    }
    let mut result = summary;
    for i in 1..n {
        summary *= power(power(a[i-1], ridx - i + 1), MOD-2);
        summary %= MOD;
        current /= a[i-1];
        while ridx < n && current * a[ridx] < LIMIT {
            current *= a[ridx];
            summary = summary * current % MOD;
            ridx += 1;
        }
        result *= summary;
        result %= MOD;
    }
    println!("{}", result);
}
0