結果

問題 No.983 Convolution
ユーザー akakimidori
提出日時 2020-02-11 13:50:11
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,098 bytes
コンパイル時間 12,379 ms
コンパイル使用メモリ 383,444 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-01 07:26:11
合計ジャッジ時間 13,692 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn gcd(a: i64, b: i64) -> i64 {
    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![0i64; n];
    for (i, s) in it.enumerate() {
        let k: i64 = 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