結果

問題 No.153 石の山
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-21 16:50:40
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,999 bytes
コンパイル時間 14,321 ms
コンパイル使用メモリ 215,728 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 08:49:39
合計ジャッジ時間 14,385 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	// https://yukicoder.me/problems/13
	// !将石头堆拆分成多个堆,当前堆的grundy数等于子状态的grundy数的异或和
	// n個の石が積まれた山が1つある。
	// A君とB君が交互に石を分けるゲームを行う。
	// !分けるときに石を2つの山か3つの山に分ける。
	// eg:
	// 2x -> x,x
	// 2x+1 -> x,x+1
	// 3x -> x,x,x
	// 3x+1 -> x,x,x+1
	// 3x+2 -> x,x+1,x+1

	// !ゲームは石を最後に分けられなくなったほうが負けである。
	// よって、この最初の石が5個のゲームの場合には、
	// ケース1のように先手のA君がまず石を3つに分ければA君が必ず勝てる。
	// A君が先手でA君もB君も勝つために最善を尽くすとき、
	// 最初のNによってA君が勝つかB君が勝つかを判定せよ。
	// 1<=n<=100

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	memo := make([]int, 110)
	for i := range memo {
		memo[i] = -1
	}

	var grundy func(state int) int
	grundy = func(state int) int {
		if memo[state] != -1 {
			return memo[state]
		}
		if state == 0 || state == 1 {
			memo[state] = 0
			return 0
		}

		nextStates := make(map[int]struct{})
		if state%2 == 0 {
			nextStates[grundy(state/2)^grundy(state/2)] = struct{}{}
		}
		if state%2 == 1 {
			nextStates[grundy(state/2)^grundy(state/2+1)] = struct{}{}
		}
		if state%3 == 0 {
			nextStates[grundy(state/3)^grundy(state/3)^grundy(state/3)] = struct{}{}
		}
		if state%3 == 1 {
			nextStates[grundy(state/3)^grundy(state/3)^grundy(state/3+1)] = struct{}{}
		}
		if state%3 == 2 {
			nextStates[grundy(state/3)^grundy(state/3+1)^grundy(state/3+1)] = struct{}{}
		}

		mex := 0
		for {
			if _, ok := nextStates[mex]; !ok {
				break
			}
			mex++
		}
		memo[state] = mex
		return mex
	}

	if grundy(n) == 0 {
		fmt.Fprintln(out, "B")
	} else {
		fmt.Fprintln(out, "A")
	}
}
0