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 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.

输入

* 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.

样例输入

2
9
10

样例输出

YES
NO

提示

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.

题意

A和B在玩游戏,给一个数a,轮到A,可以把数变成a-最大的数,a-最小的非零数,B同理,谁把值变成0谁赢

题解

观察一下可以发现,只要知道a-最大的数的sg值和a-最小的非零数的sg值,再异或1就是答案

因为先手只可以选最大或最小,后面不管怎么拿都是定死了

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int sg[1000005],n,a,t,mx,mi;
 5 void m(int x)
 6 {
 7     mx=-1,mi=10;
 8     do{
 9         t=x%10;
10         if(t)mx=max(mx,t);
11         if(t)mi=min(mi,t);
12         x/=10;
13     }while(x);
14 }
15 int main()
16 {
17     sg[0]=0;
18     for(int i=1;i<=1000000;i++)m(i),sg[i]=(sg[i-mx]^1)|(sg[i-mi]^1);
19     scanf("%d",&n);
20     while(n--)
21     {
22         scanf("%d",&a);
23         printf("%s\n",sg[a]?"YES":"NO");
24     }
25     return 0;
26 }

原文地址:https://www.cnblogs.com/taozi1115402474/p/10306547.html

时间: 2024-10-18 12:44:06

TZOJ 2703 Cow Digit Game(sg博弈)的相关文章

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 start

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).游 戏规则是这样的:双方轮流操作,将当前的数字减去一个数,这个数可

HDU 1848(sg博弈) Fibonacci again and again

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6253    Accepted Submission(s): 2603 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)

SG博弈简单题

ZOJ - 2083 - Win the Game 题目传送:Win the Game 最近正在慢慢体会博弈里面的SG函数的意义 此题是最简单的SG博弈问题,只需打个表就OK了 AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #inc

【博弈论】【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

hdu 1851(A Simple Game)(sg博弈)

A Simple Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 939 Problem Description Agrael likes play a simple game with his friend Animal during the classes. I

HDU 1536(sg博弈) S-Nim

Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows: The starting position has a number of heaps, all containing some, not necessarily equal, number of beads. The players ta

[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

ZOJ 3666 Alice and Bob (SG博弈)

题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 题意: 给一个有向图,然后A和B轮流移动棋子,棋子在每一个位置可以重叠,当某人不能走时,输! 问A和B谁赢 方法: 显然每一局游戏都是独立的,对每一局游戏异或即可 每一局游戏的结果可以用SG求,记忆化搜索之 1 int dfs(int x) 2 { 3 if (sg[x] != -1) return sg[x]; 4 bool vis[maxn]; 5 me