結果

問題 No.1770 N言っちゃダメゲーム (6)
ユーザー koba-e964koba-e964
提出日時 2022-01-02 23:00:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 3,658 bytes
コンパイル時間 3,842 ms
コンパイル使用メモリ 161,392 KB
実行使用メモリ 16,040 KB
最終ジャッジ日時 2024-04-20 08:17:17
合計ジャッジ時間 2,229 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 20 ms
16,040 KB
testcase_07 AC 18 ms
15,864 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 14 ms
12,796 KB
testcase_11 AC 14 ms
12,880 KB
testcase_12 AC 14 ms
12,832 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 17 ms
13,656 KB
testcase_29 AC 8 ms
6,940 KB
testcase_30 AC 16 ms
12,424 KB
testcase_31 AC 7 ms
6,944 KB
testcase_32 AC 20 ms
15,596 KB
testcase_33 AC 20 ms
15,876 KB
testcase_34 AC 20 ms
15,808 KB
testcase_35 AC 22 ms
15,904 KB
testcase_36 AC 21 ms
16,016 KB
testcase_37 AC 22 ms
15,936 KB
testcase_38 AC 22 ms
15,928 KB
testcase_39 AC 21 ms
15,876 KB
testcase_40 AC 23 ms
15,868 KB
testcase_41 AC 22 ms
16,036 KB
testcase_42 AC 22 ms
15,928 KB
testcase_43 AC 21 ms
15,616 KB
testcase_44 AC 16 ms
11,008 KB
testcase_45 AC 21 ms
15,608 KB
testcase_46 AC 21 ms
15,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// https://yukicoder.me/problems/no/1770 (4)
// 実験すると、last がいかなる値でも負けであるような残り数がほぼ周期 K+1 で現れているように見える。(K = 3 のとき 5, 9, 13, 17, 21, 25, 29, 33, 37, ...、K = 4 のとき 6, 11, 16, 21, 26, 31, 36, ...、K = 5 のとき 8, 14, 21, 27, 34, 40, ... など)
// K+1 が奇数の場合は、普通の N 言ったらダメゲームと同様の戦略をとれるので (K + 1 = a + b となる a = b なる 2 数が存在しないので) 1 + (K+1 の倍数) の場合に負け確定。
// losing な局面は 2K + O(1) ごとに、全 O(K^2) 状態の中で 4K + O(1) 個しか存在しないようであるため、うまく DP を行えば全列挙できる可能性がある。
// 残り a で last が b である状態を (a, b) と表すことにする。また、残りが a で、x != b であるような x に対して (a, x) が winning であることがわかっている状態を [a, b] と表すことにする。(a, b) が losing であるとき、x != b であるような x に対して (a + b, x) が winning であるため、[a + b, b] へと遷移できる。これで ある a, b, c (b != c) に対して [a, b] と [a, c] が両方わかっていれば、(a, x) はすべて winning であることが保証される。単一の b に対してのみ [a, b] がわかっている場合は、(a, b) のみが losing となる。また [a, b] がわかっている b が存在しない場合、(a, x) はすべて losing である。
// それぞれの a に対して [a, b] という知識は最大 2 個までしか意味をなさない (3 個以上あっても 2 個と変わらない) ため、配列などのデータ構造を使って、[a, b] という知識が 1 個以下であるような a を管理すればよい。初期状態は (1, 1), ..., (1, K) である。ほとんどの a に対して (a, b) が losing であるような b は 1 個以下であり、(a, x) が 1 <= x <= K なる x すべてに対して losing であるのであれば、a + 1 <= c <= a + K なる c に対しては [c, b] という知識が 1 個以上存在するため、そのような a は N/K 個程度しか存在しない。よってこれの計算量は O(N) である。
// Tags: game, nim-like
fn main() {
    let n: usize = get();
    let k: usize = get();
    let mut know = vec![vec![]; n + 1];
    for i in 1..n + 1 {
        let l = know[i].len();
        if l == 0 {
            for j in 1..min(n - i, k) + 1 {
                know[i + j].push(j);
            }
        } else if l == 1 {
            let j = know[i][0];
            if i + j <= n {
                know[i + j].push(j);
            }
        }
    }
    let mut seen = false;
    for i in 1..min(n, k + 1) {
        let l = know[n - i].len();
        if l == 0 || (l == 1 && know[n - i][0] == i) {
            println!("{}", i);
            seen = true;
        }
    }
    if !seen {
        println!("0");
    }
}
0