题目描述

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:
复制

  1. 2
  2. 9
  3. 10
输出样例#1: 复制

  1. YES
  2. 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[ i ]=mex( sg[ i-min ],sg[ i-max ]);

然后 O(1) 询问即可;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<deque>
  15. #include<stack>
  16. #include<functional>
  17. #include<sstream>
  18. //#include<cctype>
  19. //#pragma GCC optimize("O3")
  20. using namespace std;
  21. #define maxn 1000005
  22. #define inf 0x3f3f3f3f
  23. #define INF 9999999999
  24. #define rdint(x) scanf("%d",&x)
  25. #define rdllt(x) scanf("%lld",&x)
  26. #define rdult(x) scanf("%lu",&x)
  27. #define rdlf(x) scanf("%lf",&x)
  28. #define rdstr(x) scanf("%s",x)
  29. typedef long long ll;
  30. typedef unsigned long long ull;
  31. typedef unsigned int U;
  32. #define ms(x) memset((x),0,sizeof(x))
  33. const long long int mod = 1e9 + 7;
  34. #define Mod 1000000000
  35. #define sq(x) (x)*(x)
  36. #define eps 1e-3
  37. typedef pair<int, int> pii;
  38. #define pi acos(-1.0)
  39. //const int N = 1005;
  40. #define REP(i,n) for(int i=0;i<(n);i++)
  41. typedef pair<int, int> pii;
  42. inline ll rd() {
  43. ll x = 0;
  44. char c = getchar();
  45. bool f = false;
  46. while (!isdigit(c)) {
  47. if (c == '-') f = true;
  48. c = getchar();
  49. }
  50. while (isdigit(c)) {
  51. x = (x << 1) + (x << 3) + (c ^ 48);
  52. c = getchar();
  53. }
  54. return f ? -x : x;
  55. }
  56.  
  57. ll gcd(ll a, ll b) {
  58. return b == 0 ? a : gcd(b, a%b);
  59. }
  60. ll sqr(ll x) { return x * x; }
  61.  
  62. /*ll ans;
  63. ll exgcd(ll a, ll b, ll &x, ll &y) {
  64. if (!b) {
  65. x = 1; y = 0; return a;
  66. }
  67. ans = exgcd(b, a%b, x, y);
  68. ll t = x; x = y; y = t - a / b * y;
  69. return ans;
  70. }
  71. */
  72.  
  73. ll qpow(ll a, ll b, ll c) {
  74. ll ans = 1;
  75. a = a % c;
  76. while (b) {
  77. if (b % 2)ans = ans * a%c;
  78. b /= 2; a = a * a%c;
  79. }
  80. return ans;
  81. }
  82.  
  83. int n;
  84. int sg[maxn];
  85. int vis[10];
  86. int mex() {
  87. for (int i = 0;; i++) {
  88. if (!vis[i])return i;
  89. }
  90. }
  91.  
  92. void init(int Max) {
  93. sg[0] = 0;
  94. for (int i = 1; i <= Max; i++) {
  95. int tmp = i; ms(vis);
  96. int minn = 10, maxx = -1;
  97. while (tmp) {
  98. if(tmp%10!=0)
  99. minn = min(minn, tmp % 10), maxx = max(maxx, tmp % 10);
  100. tmp /= 10;
  101. }
  102. if (maxx != -1)vis[sg[i - maxx]] = 1;
  103. if (minn != 10)vis[sg[i - minn]] = 1;
  104. sg[i] = mex();
  105. }
  106. }
  107.  
  108. int main()
  109. {
  110. //ios::sync_with_stdio(0);
  111. rdint(n); init(maxn);
  112. while (n--) {
  113. int x; rdint(x);
  114. if (sg[x] == 0)cout << "NO" << endl;
  115. else cout << "YES" << endl;
  116. }
  117. return 0;
  118. }

[USACO09OPEN]牛的数字游戏Cow Digit Game 博弈的更多相关文章

  1. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  2. LuoguP2953 [USACO09OPEN]牛的数字游戏Cow Digit Game(博弈论)

    1~9显然,后面平\(A\)过去 #include <iostream> #include <cstdio> #include <cstring> #include ...

  3. 【Luogu】P2953牛的数字游戏(博弈论)

    题目链接 自己乱搞……然后一遍AC啦! 思路从基本的必胜态和必败态开始分析.我们把减去最大数得到的数叫作Max,减去最小数得到的数叫作Min. 那么开始分析. 一.0是必败态. 这个没法解释.题目就这 ...

  4. 洛谷 [P2953] 牛的数字游戏

    SG搜索 n的范围在可以接受的范围内,SG搜索即可 #include <iostream> #include <cstdio> #include <cstring> ...

  5. BZOJ3404: [Usaco2009 Open]Cow Digit Game又见数字游戏

    3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 47  Solved ...

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

    3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 72  Solved ...

  7. 【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3404 写挫好几次.... 裸的博弈论即可.. #include <cstdio> #in ...

  8. BZOJ1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5 ...

  9. bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶 ...

随机推荐

  1. c#的日志插件NLog基本使用

    本文介绍c#的日志插件NLog 安装插件 创建logger 日志级别 书写日志信息 配置 包装器 布局 安装插件 直接下载插件包 Install-Package NLog.Config 创建logge ...

  2. java 多线程系列基础篇(八)之join()、start()、run()方法

    1. join()介绍 join() 定义在Thread.java中.join() 的作用:让“主线程”等待“子线程”结束之后才能继续运行.这句话可能有点晦涩,我们还是通过例子去理解: // 主线程 ...

  3. UML设计九种图例

    一.作为一种建模语言,UML的定义包括UML语义和UML表示法两个部分. UML语义:描述基于UML的精确元模型定义. UML表示法:定义UML符号的表示法,为开发者或开发工具使用这些图形符号和文本语 ...

  4. linux命令-mke2fs

    想在磁盘下写东西,必须要先格式化 /////////////////////////////////////////////////////////////////////////////////// ...

  5. windows 10微软账户不能访问局域网共享,但是本地账户可以访问

    windows10有时候无法访问局域网的共享文件夹.会提示没有权限. 如果共享的文件夹已经设置为everyone,那么通常是windows 10用的是微软账户登录的. 有两个方案可以处理这种情况. 一 ...

  6. JS写一个简单的程序,判断年份是平年还是闰年

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. gearman client的doBackground 与doNormal方法的区别

    doNormal方法是阻塞的,需要等到worker处理完之后才返回,否则一直阻塞住; doBackground 方法是非阻塞的,只要将数据发送到gearmand之后,就立马返回,不等待worker的处 ...

  8. Python 黑客 --- 001 UNIX口令破解机

    Python 黑客 实战:UNIX口令破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V crypt 库是Python内置的库.在UNIX系统使 ...

  9. Blender 基础 骨架-02 骨架的各种呈现方式

    Blender 基础 骨架-02 - 骨架的各种呈现方式 我使用的Blender版本:Blender V 2.77 前言 在 Blender 基础 骨架-01 教程里面,将骨架和模型联系在一起,我们在 ...

  10. vray学习笔记(1)vray介绍

    vray是个什么东西? 它是个渲染器. 渲染器是个什么东西? 渲染器就是3d软件里面把模型画成一张图片的东西,渲染的过程就是把3D物体变成2D画面的过程. 模型是个什么东西? 模型就是模型,它由两部分 ...