結果

問題 No.1969 XOR Equation
ユーザー akakimidoriakakimidori
提出日時 2023-01-15 14:37:37
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 6,204 bytes
コンパイル時間 13,958 ms
コンパイル使用メモリ 378,796 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-08 17:49:05
合計ジャッジ時間 16,307 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 0 ms
5,376 KB
testcase_09 AC 23 ms
5,376 KB
testcase_10 AC 24 ms
5,376 KB
testcase_11 AC 23 ms
5,376 KB
testcase_12 AC 24 ms
5,376 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 13 ms
5,376 KB
testcase_15 AC 31 ms
5,376 KB
testcase_16 AC 31 ms
5,376 KB
testcase_17 AC 31 ms
5,376 KB
testcase_18 AC 30 ms
5,376 KB
testcase_19 AC 30 ms
5,376 KB
testcase_20 AC 19 ms
5,376 KB
testcase_21 AC 21 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 20 ms
5,376 KB
testcase_25 AC 17 ms
5,376 KB
testcase_26 AC 17 ms
5,376 KB
testcase_27 AC 31 ms
5,376 KB
testcase_28 AC 30 ms
5,376 KB
testcase_29 AC 31 ms
5,376 KB
testcase_30 AC 31 ms
5,376 KB
testcase_31 AC 31 ms
5,376 KB
testcase_32 AC 29 ms
5,376 KB
testcase_33 AC 29 ms
5,376 KB
testcase_34 AC 29 ms
5,376 KB
testcase_35 AC 43 ms
5,376 KB
testcase_36 AC 30 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin super slice ----------
pub trait SuperSlice {
    type Item;
    fn lower_bound(&self, key: &Self::Item) -> usize
    where
        Self::Item: Ord;
    fn lower_bound_by<F>(&self, f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering;
    fn lower_bound_by_key<K, F>(&self, key: &K, f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K;
    fn upper_bound(&self, key: &Self::Item) -> usize
    where
        Self::Item: Ord;
    fn upper_bound_by<F>(&self, f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering;
    fn upper_bound_by_key<K, F>(&self, key: &K, f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K;
    fn next_permutation(&mut self) -> bool
    where
        Self::Item: Ord;
    fn next_permutation_by<F>(&mut self, f: F) -> bool
    where
        F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering;
    fn prev_permutation(&mut self) -> bool
    where
        Self::Item: Ord;
}

impl<T> SuperSlice for [T] {
    type Item = T;
    fn lower_bound(&self, key: &Self::Item) -> usize
    where
        T: Ord,
    {
        self.lower_bound_by(|p| p.cmp(key))
    }
    fn lower_bound_by<F>(&self, mut f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering,
    {
        self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Greater))
            .unwrap_err()
    }
    fn lower_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        self.lower_bound_by(|p| f(p).cmp(key))
    }
    fn upper_bound(&self, key: &Self::Item) -> usize
    where
        T: Ord,
    {
        self.upper_bound_by(|p| p.cmp(key))
    }
    fn upper_bound_by<F>(&self, mut f: F) -> usize
    where
        F: FnMut(&Self::Item) -> std::cmp::Ordering,
    {
        self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Less))
            .unwrap_err()
    }
    fn upper_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize
    where
        K: Ord,
        F: FnMut(&Self::Item) -> K,
    {
        self.upper_bound_by(|p| f(p).cmp(key))
    }
    fn next_permutation(&mut self) -> bool
    where
        T: Ord,
    {
        self.next_permutation_by(|a, b| a.cmp(b))
    }
    fn next_permutation_by<F>(&mut self, mut f: F) -> bool
    where
        F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering,
    {
        use std::cmp::Ordering::*;
        if let Some(x) = self.windows(2).rposition(|a| f(&a[0], &a[1]) == Less) {
            let y = self.iter().rposition(|b| f(&self[x], b) == Less).unwrap();
            self.swap(x, y);
            self[(x + 1)..].reverse();
            true
        } else {
            self.reverse();
            false
        }
    }
    fn prev_permutation(&mut self) -> bool
    where
        T: Ord,
    {
        self.next_permutation_by(|a, b| a.cmp(b).reverse())
    }
}
// ---------- end super slice ----------
// ---------- 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;

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 a = sc.next_vec::<u64>(n);
        let mut a = a.into_iter().map(|a| (a, 0)).collect::<Vec<_>>();
        a.sort_by_key(|p| p.1);
        let inf = std::u64::MAX / 2;
        let mut dp = vec![inf; n + 1];
        dp[n] = 0;
        for i in 0..61 {
            let mut b = a
                .iter()
                .map(|a| (a.0, a.0 & ((2 << i) - 1)))
                .collect::<Vec<_>>();
            b.sort_by_key(|p| p.1);
            let mut s = b.iter().fold(0, |s, b| s + (b.1 >> i & 1));
            let y = y >> i & 1;
            let mut next = vec![inf; n + 1];
            for (carry, dp) in dp.iter().enumerate().rev() {
                if carry < n {
                    s += 1;
                }
                if *dp < inf {
                    for v in 0..2 {
                        let add = *dp + (v << i);
                        let pos = b.lower_bound_by_key(&((2 << i) - add), |b| b.1);
                        let xor = s + v * n as u64 - 2 * (n - pos) as u64;
                        if xor & 1 == y {
                            next[pos].chmin(add);
                        }
                    }
                }
            }
            dp = next;
            a = b;
        }
        if dp[n] < inf {
            writeln!(out, "{}", dp[n]).ok();
        } else {
            writeln!(out, "-1").ok();
        }
    }
}
0