1. 程式人生 > >P2953 [USACO09OPEN]牛的數字遊戲Cow Digit Game

P2953 [USACO09OPEN]牛的數字遊戲Cow Digit Game

最大數 sdi play tee digi nes tin hat lin

P2953 [USACO09OPEN]牛的數字遊戲Cow Digit Game

題目描述

Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory.

Game i starts with an integer N_i (1 <= N_i <= 1,000,000). Bessie goes first, and then the two players alternate turns. On each turn, a player can subtract either the largest digit or the smallest non-zero digit from the current number to obtain a new number. For example, from 3014 we may subtract either 1 or 4 to obtain either 3013 or 3010, respectively. The game continues until the number becomes 0, at which point the last player to have taken a turn is the winner.

Bessie and FJ play G (1 <= G <= 100) games. Determine, for each game, whether Bessie or FJ will win, assuming that both play perfectly (that is, on each turn, if the current player has a move that will guarantee his or her win, he or she will take it).

Consider a sample game where N_i = 13. Bessie goes first and takes 3, leaving 10. FJ is forced to take 1, leaving 9. Bessie takes the remainder and wins the game.

貝茜和約翰在玩一個數字遊戲.貝茜需要你幫助她.

遊戲一共進行了G(1≤G≤100)場.第i場遊戲開始於一個正整數Ni(l≤Ni≤1,000,000).遊

戲規則是這樣的:雙方輪流操作,將當前的數字減去一個數,這個數可以是當前數字的最大數碼,也可以是最小的非0數碼.比如當前的數是3014,操作者可以減去1變成3013,也可以減去4變成3010.若幹次操作之後,這個數字會變成0.這時候不能再操作的一方為輸家. 貝茜總是先開始操作.如果貝茜和約翰都足夠聰明,執行最好的策略.請你計算最後的贏家.

比如,一場遊戲開始於13.貝茜將13減去3變成10.約翰只能將10減去1變成9.貝茜再將9減去9變成0.最後貝茜贏.

輸入輸出格式

輸入格式:

  • Line 1: A single integer: G

  • Lines 2..G+1: Line i+1 contains the single integer: N_i

輸出格式:

  • Lines 1..G: Line i contains ‘YES‘ if Bessie can win game i, and ‘NO‘ otherwise.

輸入輸出樣例

輸入樣例#1:
2 
9 
10 
輸出樣例#1:
YES 
NO 

說明

For the first game, Bessie simply takes the number 9 and wins. For the second game, Bessie must take 1 (since she cannot take 0), and then FJ can win by taking 9.

博弈論 用到了sg函數和sg定理 然而我一開始並沒有看出來

亂搞拿了40 ,這數據是要多水啊

組合博弈:三大遊戲

博弈論:sg函數

技術分享
 1 #include <cstdio>
 2 #include <cctype>
 3 
 4 const int MAXN=1000010;
 5 
 6 int n,t;
 7 
 8 int sg[MAXN];
 9 
10 inline void read(int&x) {
11     int f=1;register char c=getchar();
12     for(x=0;!isdigit(c);c==-&&(f=-1),c=getchar());
13     for(;isdigit(c);x=x*10+c-48,c=getchar());
14     x=x*f;
15 }
16 
17 int hh() {
18     read(n);
19     for(int mn,mx,t,i=1;i<MAXN-1;++i) {
20         mn=9;mx=0;t=i;
21         while(t) {
22             int b=t%10;
23             if(b) {mx=mx<b?b:mx;mn=mn>b?b:mn;}
24             t/=10;
25         }
26         sg[i]=(sg[i-mx]&sg[i-mn])^1;
27     }
28     for(int t,i=1;i<=n;++i) {
29         read(t);
30         sg[t]?printf("YES\n"):printf("NO\n");
31     }
32     return 0;
33 }
34 
35 int sb=hh();
36 int main(int argc,char**argv) {;} 
代碼

P2953 [USACO09OPEN]牛的數字遊戲Cow Digit Game