[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.
输入输出样例
说明
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) 询问即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n;
int sg[maxn];
int vis[10];
int mex() {
for (int i = 0;; i++) {
if (!vis[i])return i;
}
} void init(int Max) {
sg[0] = 0;
for (int i = 1; i <= Max; i++) {
int tmp = i; ms(vis);
int minn = 10, maxx = -1;
while (tmp) {
if(tmp%10!=0)
minn = min(minn, tmp % 10), maxx = max(maxx, tmp % 10);
tmp /= 10;
}
if (maxx != -1)vis[sg[i - maxx]] = 1;
if (minn != 10)vis[sg[i - minn]] = 1;
sg[i] = mex();
}
} int main()
{
//ios::sync_with_stdio(0);
rdint(n); init(maxn);
while (n--) {
int x; rdint(x);
if (sg[x] == 0)cout << "NO" << endl;
else cout << "YES" << endl;
}
return 0;
}
[USACO09OPEN]牛的数字游戏Cow Digit Game 博弈的更多相关文章
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- LuoguP2953 [USACO09OPEN]牛的数字游戏Cow Digit Game(博弈论)
1~9显然,后面平\(A\)过去 #include <iostream> #include <cstdio> #include <cstring> #include ...
- 【Luogu】P2953牛的数字游戏(博弈论)
题目链接 自己乱搞……然后一遍AC啦! 思路从基本的必胜态和必败态开始分析.我们把减去最大数得到的数叫作Max,减去最小数得到的数叫作Min. 那么开始分析. 一.0是必败态. 这个没法解释.题目就这 ...
- 洛谷 [P2953] 牛的数字游戏
SG搜索 n的范围在可以接受的范围内,SG搜索即可 #include <iostream> #include <cstdio> #include <cstring> ...
- BZOJ3404: [Usaco2009 Open]Cow Digit Game又见数字游戏
3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 47 Solved ...
- 3404: [Usaco2009 Open]Cow Digit Game又见数字游戏
3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 72 Solved ...
- 【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)
http://www.lydsy.com/JudgeOnline/problem.php?id=3404 写挫好几次.... 裸的博弈论即可.. #include <cstdio> #in ...
- BZOJ1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5 ...
- bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶 ...
随机推荐
- [vijos1246]文科生的悲哀(二) 动态规划
背景 化学不及格的Matrix67无奈选择了文科.他必须硬着头皮艰难地进行着文科的学习. 描述 这学期的政治.历史和地理课本各有n章.每一科的教学必须按章节从前往后依次进行.若干章政治.若干章历史和若 ...
- SpringMVC + AJAX 实现多文件异步上传
转自:https://www.jianshu.com/p/f3987f0f471f 今天,我就这个问题来写一篇如何用 SpringMVC + AJAX 实现的多文件异步上传功能.基本的代码还是沿用上篇 ...
- Using JConsole
Using JConsole 转自 https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html Th ...
- HDU 4348(主席树 标记永久化)
题面一看就是裸的数据结构题,而且一看就知道是主席树... 一共四种操作:1:把区间[l, r]的数都加上d,并且更新时间.2:查询当前时间的区间和.3:查询历史时间的区间和.4:时光倒流到某个时间. ...
- IO流对文件的读取操作
/*1. 在当前项目的根目录下有一个名为“info.txt”的文件,里面存放的内容如下(可手动创建录入,不需要使用IO流): 2. 利用IO流的知识读取info.txt文件的内容, 在控制台上打印大写 ...
- Elasticsearch - 环境准备
Precondition: Ubuntu OS 环境准备: 1. JAVA_HOME 1.1 Download the jdk8 (jdk-8u25-linux-x64.tar.gz) from of ...
- android sdk更新源
什么是Android SDK: SDK:(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集 ...
- Linux 性能调优
一.简介 有些时候,我们特别关注程序的性能,特别是底层软件,比如驱动程序,OS等.为了更好的优化程序性能,我们必须找到性能瓶颈点,"好钢用在刀刃上"才能取得好的效果,否则可能白做工 ...
- p3584 [POI2015]LAS
传送门 分析 f[i][S](S∈[0,4])表示第iii个食物没有被选/左边选/右边选/同时选的状态是由哪一个状态转移来的 我们需要满足两个条件: 每个人只能选择一个 改变选择之后不会比当前获得热 ...
- ZROI2018提高day4t3
传送门 分析 我们假设如果一个点是0则它的值为-1,如果一个点是1则值为1,则一个区间的答案便是max(pre[i]+sur[i]),这里的pre[i]表示此区间i点和它之前的的前缀的最大值,sur[ ...