TOJ 2703: Cow Digit Game

2703: Cow Digit Game

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 1            Accepted:1

Description

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.

Input

* Line 1: A single integer: G

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

Output

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

Sample Input

2
9
10

Sample Output

YES
NO

Hint

OUTPUT DETAILS:

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.

Source

USACO Open 2009

简单的博弈,sg函数

把一个数减去这个数非0的最大值或者最小值,分析其必胜

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N=1e6+10;
int sg[N];
int main(){
for(int i=1;i<=1e6;i++){
int ma=0,mi=9,t=i;
while(t){
    int b=t%10;
    if(b){
    ma=max(b,ma);
    mi=min(b,mi);}
    t/=10;
}
sg[i]=(sg[i-ma]&sg[i-mi])^1;
}
int t;
scanf("%d",&t);
while(t--){
    int n;
    scanf("%d",&n);
    printf("%s",sg[n]?"YES\n":"NO\n");
}
return 0;}
时间: 2024-10-21 14:22:21

TOJ 2703: Cow Digit Game的相关文章

TZOJ 2703 Cow Digit Game(sg博弈)

描述 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

3404: [Usaco2009 Open]Cow Digit Game又见数字游戏

3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 48[Submit][Status][Discuss] Description 贝茜和约翰在玩一个数字游戏.贝茜需要你帮助她. 游戏一共进行了G(1≤G≤100)场.第i场游戏开始于一个正整数Ni(l≤Ni≤1,000,000).游 戏规则是这样的:双方轮流操作,将当前的数字减去一个数,这个数可

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 c

[bzoj3404] [Usaco2009 Open]Cow Digit Game又见数字游戏

直接把所有数的sg值算出来就行了. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1002333; 7 int sg[maxn]; 8 int st[8]; 9 int i,j,k,n,m; 10 11 int ra;char rx; 12 inline int

toj 2829 cow counting

一开始想多了,以为应该做个数位dp的,后来想了想也不过100W的数据,直接暴力好像也不慢,于是暴力就过了,还挺快. 1 #include <iostream> 2 using namespace std; 3 4 bool judge( int x, int d ) 5 { 6 while ( x ) 7 { 8 int r = x % 10; 9 if ( r == d ) return false; 10 x = x / 10; 11 } 12 return true; 13 } 14 1

【博弈论】【SG函数】bzoj3404 [Usaco2009 Open]Cow Digit Game又见数字游戏

#include<cstring> #include<cstdio> #include<algorithm> #include<set> using namespace std; int m,n; int SG[1000001]; int sg(int x) { if(SG[x]!=-1) return SG[x]; if(!x) return SG[x]=0; set<int>S; int maxv=0,minv=2147483647; int

【BZOJ】【3404】【USACO2009 Open】Cow Digit Game又见数字游戏

博弈论 Orz ZYF 从前往后递推……反正最大才10^6,完全可以暴力预处理每个数的状态是必胜还是必败(反正才两个后继状态),然后O(1)查询……我是SB 1 /************************************************************** 2 Problem: 3404 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:880 ms 7 Memory:5180 kb 8 *******

Digit Generator(水)

题目链接:http://acm.tju.edu.cn/toj/showp2502.html2502.   Digit Generator Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 2426   Accepted Runs: 1579 For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits.

Usaco Open09 Silver

********************************************************************** SILVER PROBLEMS ********************************************************************** Four problems numbered 6 through 9 *********************************************************