結果

問題 No.983 Convolution
ユーザー akakimidoriakakimidori
提出日時 2020-02-11 14:19:16
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,103 bytes
コンパイル時間 12,620 ms
コンパイル使用メモリ 383,300 KB
実行使用メモリ 7,556 KB
最終ジャッジ日時 2024-10-01 07:32:31
合計ジャッジ時間 13,382 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn gcd(a: i128, b: i128) -> i128 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let n = n.next_power_of_two();
    let m = n.trailing_zeros() as usize;
    let mut a = vec![0i128; n];
    for (i, s) in it.enumerate() {
        let k: i128 = s.parse().unwrap();
        if k >= 0 {
            a[i] = k;
        }
    }
    for i in 0..m {
        for j in 0..n {
            if (j >> i) & 1 == 1 {
                a[j ^ (1 << i)] += a[j];
            }
        }
    }
    for i in 0..n {
        a[i] *= a[i];
    }
    for i in 0..m {
        for j in 0..n {
            if (j >> i) & 1 == 1 {
                a[j ^ (1 << i)] -= a[j];
            }
        }
    }
    let mut ans = 0;
    for a in a {
        ans = gcd(ans, a);
    }
    if ans == 0 {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}

fn main() {
    run();
}
0