結果

問題 No.1538 引きこもりさんは引き算が得意。
ユーザー 57tggx57tggx
提出日時 2021-05-20 18:02:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 3,329 bytes
コンパイル時間 4,916 ms
コンパイル使用メモリ 164,392 KB
実行使用メモリ 54,412 KB
最終ジャッジ日時 2023-08-20 22:30:31
合計ジャッジ時間 8,922 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 220 ms
54,388 KB
testcase_38 AC 211 ms
54,360 KB
testcase_39 AC 211 ms
54,384 KB
testcase_40 AC 263 ms
54,388 KB
testcase_41 AC 264 ms
54,412 KB
testcase_42 AC 188 ms
54,400 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 148 ms
28,184 KB
testcase_45 AC 152 ms
28,288 KB
testcase_46 AC 89 ms
4,376 KB
testcase_47 AC 49 ms
15,024 KB
testcase_48 AC 208 ms
54,332 KB
testcase_49 AC 164 ms
28,088 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 208 ms
54,396 KB
testcase_52 AC 211 ms
54,320 KB
testcase_53 AC 214 ms
54,400 KB
testcase_54 AC 215 ms
54,400 KB
testcase_55 AC 214 ms
54,356 KB
testcase_56 AC 214 ms
54,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),* $(,)*) => {
        #[cfg(debug_assertions)]
        eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*);
    };
}

#[derive(Debug)]
struct Input {
    n: usize,
    k: i64,
    a: Vec<i64>,
}

fn main() {
    let Input { n, k, a } = Input::read(std::io::stdin().lock());

    if a.iter().any(|&x| x == k) {
        println!("Yes");
        return;
    }

    let mut w = std::collections::HashMap::<i64, i64>::new();
    let mut bad: i64 = 0;

    for i in 0..1 << n {
        let mut sum = 0;
        let mut unused = 0;
        for (j, x) in a.iter().enumerate() {
            if i >> j & 1 == 1 {
                sum += x;
            } else {
                unused += 1;
            }
        }
        *w.entry(sum).or_insert(0) += 1;
        if sum == k {
            bad += 1 << unused;
        }
        if sum == -k {
            bad += 1 << unused;
        }
    }

    debug!(w, bad);

    let mut count = 0;
    for (s, count1) in &w {
        if let Some(count2) = w.get(&(s + k)) {
            count += count1 * count2;
        }
    }

    if k == 0 {
        debug!(count, bad, 1 << n);
        count += 1 << n;
    }
    count -= bad;
    debug!(count);

    println!("{}", if count > 0 { "Yes" } else { "No" });
}

impl Input {
    fn read<T: std::io::BufRead>(mut input: T) -> Input {
        let (n, k) = {
            let mut buffer = String::new();
            input.read_line(&mut buffer).unwrap();
            match &split(&buffer).unwrap()[..] {
                &[n, k] => (n.parse().unwrap(), k.parse().unwrap()),
                _ => panic!("input format error: N K"),
            }
        };
        let a: Vec<i64> = {
            let mut buffer = String::new();
            input.read_line(&mut buffer).unwrap();
            split(&buffer)
                .unwrap()
                .into_iter()
                .map(|x| x.parse().unwrap())
                .collect()
        };

        assert!(matches!(n, 2..=20));
        assert!(matches!(k, -1_000_000_000..=1_000_000_000));
        assert_eq!(n, a.len());
        assert!(a
            .iter()
            .all(|x| matches!(x, -1_000_000_000..=1_000_000_000)));

        Input { n: n, k: k, a: a }
    }
}

fn split(s: &str) -> Option<Vec<&str>> {
    enum State {
        Word(usize),
        Space,
        End,
    }
    let mut state = State::Word(0);
    let mut ret = Vec::new();
    for (i, c) in s.char_indices() {
        let prev = match state {
            State::End => return None,
            State::Word(i) => i,
            State::Space => {
                state = State::Word(i);
                i
            }
        };
        if c == ' ' || c == '\n' {
            ret.push(&s[prev..i]);
            state = if c == ' ' { State::Space } else { State::End };
        }
    }
    matches!(state, State::End).then(|| ret)
}

#[test]
fn test_split() {
    assert_eq!(split("word\n"), Some(vec!["word"]));
    assert_eq!(
        split("many words separated\n"),
        Some(vec!["many", "words", "separated"])
    );
    assert_eq!(
        split(" extra  spaces \n"),
        Some(vec!["", "extra", "", "spaces", ""])
    );
    assert_eq!(split("no line feed"), None);
    assert_eq!(split("extra characters\nafter line feed"), None);
}
0