結果

問題 No.1969 XOR Equation
ユーザー akakimidoriakakimidori
提出日時 2022-06-03 21:33:21
言語 Rust
(1.77.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,820 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 189,680 KB
実行使用メモリ 397,544 KB
最終ジャッジ日時 2023-10-21 02:30:33
合計ジャッジ時間 6,291 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,696 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 0 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 38 ms
4,348 KB
testcase_10 AC 38 ms
4,348 KB
testcase_11 AC 42 ms
4,348 KB
testcase_12 AC 43 ms
4,348 KB
testcase_13 AC 16 ms
4,348 KB
testcase_14 AC 9 ms
4,348 KB
testcase_15 AC 22 ms
5,176 KB
testcase_16 AC 44 ms
7,988 KB
testcase_17 AC 40 ms
8,308 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 29 ms
4,348 KB
testcase_21 AC 16 ms
4,348 KB
testcase_22 AC 11 ms
4,348 KB
testcase_23 AC 36 ms
4,348 KB
testcase_24 AC 12 ms
4,348 KB
testcase_25 AC 16 ms
5,324 KB
testcase_26 AC 18 ms
4,468 KB
testcase_27 AC 46 ms
9,320 KB
testcase_28 AC 28 ms
5,832 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 4 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 4 ms
4,348 KB
testcase_35 AC 55 ms
4,348 KB
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let t: u32 = sc.next();
    for _ in 0..t {
        let n: usize = sc.next();
        let y: u64 = sc.next();
        let mut a = sc.next_vec::<u64>(n);
        a.sort();
        let mut dp = Map::new();
        dp.insert(a, 0);
        for i in 0..61 {
            let y = y >> i & 1;
            let mut next = Map::new();
            for (a, v) in dp {
                for add in 0..2 {
                    let v = v + (add << i);
                    let mut a = a.iter().map(|a| *a + add).collect::<Vec<_>>();
                    if a.iter().fold(0, |s, a| s ^ *a) & 1 == y {
                        a.iter_mut().for_each(|a| *a /= 2);
                        next.entry(a).or_insert(std::u64::MAX).chmin(v);
                    }
                }
            }
            dp = next;
        }
        if let Some(v) = dp.iter().filter(|p| p.0.iter().all(|p| *p == 0)).map(|p| *p.1).min() {
            writeln!(out, "{}", v).ok();
        } else {
            writeln!(out, "-1").ok();
        }
    }
}
0