結果

問題 No.715 集合と二人ゲーム
ユーザー koba-e964koba-e964
提出日時 2021-12-14 13:28:41
言語 Rust
(1.77.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,635 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 150,636 KB
実行使用メモリ 7,692 KB
最終ジャッジ日時 2023-09-30 22:08:21
合計ジャッジ時間 4,926 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 10 ms
4,380 KB
testcase_20 AC 12 ms
4,376 KB
testcase_21 AC 12 ms
4,380 KB
testcase_22 AC 13 ms
4,376 KB
testcase_23 AC 13 ms
4,376 KB
testcase_24 AC 14 ms
4,376 KB
testcase_25 AC 14 ms
4,380 KB
testcase_26 AC 13 ms
4,380 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 16 ms
4,376 KB
testcase_29 AC 16 ms
4,380 KB
testcase_30 AC 17 ms
4,376 KB
testcase_31 AC 15 ms
4,380 KB
testcase_32 AC 14 ms
4,376 KB
testcase_33 AC 16 ms
4,376 KB
testcase_34 AC 14 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 109 ms
7,380 KB
testcase_51 AC 109 ms
7,236 KB
testcase_52 AC 106 ms
7,120 KB
testcase_53 AC 104 ms
7,692 KB
testcase_54 AC 106 ms
7,684 KB
testcase_55 AC 108 ms
7,432 KB
testcase_56 AC 103 ms
7,232 KB
testcase_57 AC 96 ms
6,904 KB
testcase_58 AC 108 ms
7,388 KB
testcase_59 AC 108 ms
7,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// https://yukicoder.me/problems/no/715 (3.5)
// 全部離れていたら Nim なので N の偶奇。離れているグループ同士は干渉しないので、連続する整数ごとにグループを作って Nim で、しかもグループの Grundy 数は長さのみで決まる。DP?
// N = 5 * 10^5 で実験すると、dp[i] (0 <= i <= N) の最大値は 9 だった。また OEIS で調べると http://oeis.org/A002187 と同一であることがわかった。これは 5 個の n を除いて dp[n] = dp[n + 34] を満たす。これが満たされない n の最大値は 51 なので、n <= 51 + 34 = 85 では dp[n] を計算し、それ以外では (n-52) % 34 を使用してより小さい n に帰着する。
// Tags: dawsons-chess, oeis, nim, octal-game
// ref: http://oeis.org/A002187
fn main() {
    input! {
        n: usize,
        a: [i64; n],
    }
    let mut a = a;
    a.sort();
    let lim = 86;
    let mut dp = vec![0; lim];
    dp[1] = 1;
    for i in 2..lim {
        let mut seen = 1 << dp[i - 2];
        for j in 0..i - 2 {
            let x = dp[j] ^ dp[i - 3 - j];
            seen |= 1 << x;
        }
        let mut mex = 0;
        while (seen & 1 << mex) != 0 { mex += 1; }
        dp[i] = mex;
    }
    let gr = |mut a: usize| -> i32 {
        if a >= lim {
            a = (a - lim + 34) % 34 + lim - 34;
        }
        dp[a]
    };
    let mut last = -1;
    let mut c = 0;
    let mut ans = 0;
    for a in a {
        if last + 1 != a {
            ans ^= gr(c);
            c = 1;
        } else {
            c += 1;
        }
        last = a;
    }
    ans ^= gr(c);
    println!("{}", if ans == 0 { "Second" } else { "First" });
}
0