結果

問題 No.715 集合と二人ゲーム
ユーザー tubo28tubo28
提出日時 2017-10-04 14:28:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,337 bytes
コンパイル時間 1,352 ms
コンパイル使用メモリ 161,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 23:03:26
合計ジャッジ時間 5,021 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 9 ms
4,376 KB
testcase_20 AC 11 ms
4,376 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 12 ms
4,380 KB
testcase_24 AC 13 ms
4,376 KB
testcase_25 AC 13 ms
4,376 KB
testcase_26 AC 13 ms
4,380 KB
testcase_27 AC 14 ms
4,376 KB
testcase_28 AC 14 ms
4,376 KB
testcase_29 AC 15 ms
4,380 KB
testcase_30 AC 16 ms
4,376 KB
testcase_31 AC 14 ms
4,376 KB
testcase_32 AC 12 ms
4,376 KB
testcase_33 AC 15 ms
4,380 KB
testcase_34 AC 13 ms
4,376 KB
testcase_35 AC 1 ms
4,380 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,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 98 ms
4,380 KB
testcase_51 AC 95 ms
4,380 KB
testcase_52 AC 96 ms
4,380 KB
testcase_53 AC 95 ms
4,376 KB
testcase_54 AC 100 ms
4,376 KB
testcase_55 AC 97 ms
4,380 KB
testcase_56 AC 95 ms
4,376 KB
testcase_57 AC 87 ms
4,380 KB
testcase_58 AC 97 ms
4,376 KB
testcase_59 AC 96 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// OEIS A002187 https://oeis.org/A002187

use std::io::*;
use std::str::*;

fn readw<T: FromStr, R: Read>(s: &mut R) -> Option<T> {
    let s = s.bytes().map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    if s.is_empty() {
        None
    } else {
        s.parse::<T>().ok()
    }
}

const MAX_N: usize = 150;

fn read<T: FromStr, R: Read>(s: &mut R) -> T {
    readw(s).unwrap_or_else(|| std::process::exit(0))
}

fn grundy(n: u32, dp: &mut Vec<Option<u32>>) -> u32 {
    if n < 100 {
        grundy_rec(n, dp)
    } else {
        grundy_rec((n - 100) % 34 + 100, dp)
    }
}

fn grundy_rec(n: u32, dp: &mut Vec<Option<u32>>) -> u32 {
    if let Some(res) = dp[n as usize] {
        res
    } else {
        let res = if n <= 1 {
            n
        } else if n == 2 {
            1
        } else {
            let mut s = vec![false; MAX_N];
            if n >= 4 {
                for i in 1..n-3 {
                    s[(grundy_rec(i, dp) ^ grundy_rec(n - 3 - i, dp)) as usize] = true;
                }
            }
            s[grundy_rec(n - 2, dp) as usize] = true;
            s[grundy_rec(n - 3, dp) as usize] = true;
            (0..MAX_N).find(|&x| !s[x]).unwrap() as u32
        };
        dp[n as usize] = Some(res);
        // println!("{} {}", n, res);
        res
    }
}

fn solve() {
    // let mut dp = vec![None; 100];
    // for i in 1..100 {
    //     print!("{}, ", grundy(i, &mut dp));
    // }

    let s = stdin();
    let s = s.lock();
    let s = &mut BufReader::new(s);

    loop {
        let n: usize = read(s);
        let mut a: Vec<u32> = (0..n).map(|_| read(s)).collect();
        a.sort();

        let mut g = 0;
        let mut dp = vec![None; n + 1];
        let mut i = 0;
        while i < n {
            let mut j = i;
            while j + 1 < n && a[j + 1] == a[j] + 1 {
                j += 1;
            }
            // [i,j]
            let k = j - i + 1;
            g ^= grundy(k as u32, &mut dp);
            i = j + 1;
        }

        println!("{}", if g == 0 { "Second" } else { "First" });
    }
}

fn main() {
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(solve).unwrap().join().unwrap();
}
0