結果

問題 No.153 石の山
ユーザー uafr_csuafr_cs
提出日時 2015-09-11 17:22:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 129 ms / 5,000 ms
コード長 1,132 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 78,732 KB
実行使用メモリ 41,472 KB
最終ジャッジ日時 2024-04-22 15:33:54
合計ジャッジ時間 7,353 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
41,008 KB
testcase_01 AC 128 ms
41,232 KB
testcase_02 AC 126 ms
41,204 KB
testcase_03 AC 127 ms
41,136 KB
testcase_04 AC 127 ms
40,956 KB
testcase_05 AC 119 ms
40,716 KB
testcase_06 AC 116 ms
39,832 KB
testcase_07 AC 129 ms
40,940 KB
testcase_08 AC 126 ms
41,076 KB
testcase_09 AC 127 ms
41,056 KB
testcase_10 AC 114 ms
39,900 KB
testcase_11 AC 113 ms
39,984 KB
testcase_12 AC 125 ms
41,128 KB
testcase_13 AC 126 ms
41,472 KB
testcase_14 AC 114 ms
39,872 KB
testcase_15 AC 127 ms
40,980 KB
testcase_16 AC 114 ms
39,860 KB
testcase_17 AC 118 ms
40,148 KB
testcase_18 AC 126 ms
40,992 KB
testcase_19 AC 126 ms
41,208 KB
testcase_20 AC 122 ms
41,028 KB
testcase_21 AC 116 ms
40,120 KB
testcase_22 AC 124 ms
40,844 KB
testcase_23 AC 129 ms
41,340 KB
testcase_24 AC 127 ms
41,016 KB
testcase_25 AC 115 ms
40,120 KB
testcase_26 AC 114 ms
39,676 KB
testcase_27 AC 127 ms
40,800 KB
testcase_28 AC 126 ms
41,408 KB
testcase_29 AC 115 ms
39,856 KB
testcase_30 AC 128 ms
41,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {

	public static int grundy(int n, int[] memo){
		if(memo[n] >= 0){ return memo[n]; }
		if(n == 1){ return memo[n] = 0; }
		
		TreeSet<Integer> set = new TreeSet<Integer>();
		
		if(n >= 2){
			set.add(grundy(n / 2, memo) ^ grundy(n / 2 + (n % 2), memo));
		}
		
		if(n >= 3){
			if(n % 3 == 0){
				set.add(grundy(n / 3, memo) ^ grundy(n / 3, memo) ^ grundy(n / 3, memo));
			}else if(n % 3 == 1){
				set.add(grundy(n / 3, memo) ^ grundy(n / 3, memo) ^ grundy(n / 3 + 1, memo));
			}else{
				set.add(grundy(n / 3, memo) ^ grundy(n / 3 + 1, memo) ^ grundy(n / 3 + 1, memo));
			}
		}
		
		for(int i = 0; ; i++){
			if(!set.contains(i)){
				return memo[n] = i;
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		int[] memo = new int[N + 1];
		Arrays.fill(memo, -1);
		
		System.out.println(grundy(N, memo) == 0 ? "B" : "A");
		//System.out.println(Arrays.toString(memo));
	}

}
0