結果

問題 No.1240 Or Sum of Xor Pair
ユーザー akakimidoriakakimidori
提出日時 2020-09-26 20:02:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 2,957 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 154,272 KB
実行使用メモリ 6,284 KB
最終ジャッジ日時 2023-09-12 04:36:00
合計ジャッジ時間 6,385 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 10 ms
4,376 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 19 ms
4,376 KB
testcase_13 AC 18 ms
4,380 KB
testcase_14 AC 15 ms
4,376 KB
testcase_15 AC 84 ms
6,216 KB
testcase_16 AC 73 ms
6,272 KB
testcase_17 AC 68 ms
6,268 KB
testcase_18 AC 73 ms
6,276 KB
testcase_19 AC 66 ms
6,264 KB
testcase_20 AC 83 ms
6,164 KB
testcase_21 AC 76 ms
6,280 KB
testcase_22 AC 82 ms
6,280 KB
testcase_23 AC 60 ms
6,268 KB
testcase_24 AC 76 ms
6,164 KB
testcase_25 AC 51 ms
6,284 KB
testcase_26 AC 79 ms
6,236 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 12 ms
5,636 KB
testcase_30 AC 15 ms
4,376 KB
testcase_31 AC 19 ms
4,380 KB
testcase_32 AC 34 ms
5,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

fn sum(a: &[u64], b: &[u64]) -> u64 {
    let mut cnt = [0; 18];
    for &b in b.iter() {
        for (i, c) in cnt.iter_mut().enumerate() {
            *c += (b >> i as u64) & 1;
        }
    }
    let all = b.len() as u64;
    let mut ans = 0;
    for &a in a.iter() {
        for (i, c) in cnt.iter().enumerate() {
            let i = i as u64;
            if a >> i & 1 == 1 {
                ans += all << i;
            } else {
                ans += *c << i;
            }
        }
    }
    ans
}

// a xor b < x なペアの a | b の和を返す
// a, bはソート済み, 任意のペアについて (a ^ b) >> w == x >> w
fn calc(a: &[u64], b: &[u64], w: u64, x: u64) -> u64 {
    if a.is_empty() || b.is_empty() || w == 0 {
        return 0;
    }
    let w = w - 1;
    let (al, ar) = {
        let mut k = 0;
        while k < a.len() && a[k] >> w & 1 == 0 {
            k += 1;
        }
        a.split_at(k)
    };
    let (bl, br) = {
        let mut k = 0;
        while k < b.len() && b[k] >> w & 1 == 0 {
            k += 1;
        }
        b.split_at(k)
    };
    let mut res = 0;
    if x >> w & 1 == 1 {
        res += sum(al, bl);
        res += sum(ar, br);
        res += calc(al, br, w, x);
        res += calc(ar, bl, w, x);
    } else {
        res += calc(al, bl, w, x);
        res += calc(ar, br, w, x);
    }
    res
}

fn run() {
    input! {
        n: usize,
        x: u64,
        a: [u64; n],
    }
    let mut a = a;
    a.sort();
    let mut ans = calc(&a, &a, 19, x);
    for a in a {
        ans -= a;
    }
    ans /= 2;
    println!("{}", ans);
}

fn main() {
    run();
}
0