USACO 2009 Open Silver 3.Cow Digit Game简单博弈论
来源:互联网 发布:网络词汇大全 编辑:程序博客网 时间:2024/06/05 03:11
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 larges 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
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.
题意:对于一个初始的数n,每次可以减去这个数的数位中最大的那一位,或是最小的非零的那一位,如3014中最大的是4,最小非零的是1,可以得到3014-1=3013或3014-4=3010,两个人轮流操作,减到到0的人获胜。
最近迷上了这些简单的博弈论了呢~
显然,当轮到一个人时,n已经为0,则此时就是一个必败态,令必败态的sg值为0,必胜态的sg值为1,那么我们就可以从小到大递推了。
设当前数i中最大的一位是maxi,最小的非零位是mini,那么若i-maxi和i-mini都对应一个必胜态,那么i就是一个必败态;若i-maxi和i-mini中有一个是必败态,那么i就是一个必胜态。
至于找最大位和最小非零位,鉴于数据范围不是太变态,暴力拆分就可以了。
代码:
#include<stdio.h>int sg[1000010],num[10],len;int maxi,mini;int main(){ sg[0]=0; int i,t,n; len=0; for(i=1;i<=1000000;i++) { t=i; maxi=0; mini=10; while(t) { if(t%10>maxi) maxi=t%10; if(t%10>0&&t%10<mini) mini=t%10; t/=10; } sg[i]=(sg[i-maxi]&sg[i-mini])^1; } scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&t); if(sg[t]) printf("YES\n"); else printf("NO\n"); } return 0;}
- USACO 2009 Open Silver 3.Cow Digit Game简单博弈论
- bzoj3404 [Usaco2009 Open]Cow Digit Game又见数字游戏
- [BZOJ3404][Usaco2009 Open]Cow Digit Game又见数字游戏
- bzoj3404 [Usaco2009 Open]Cow Digit Game又见数字游戏
- USACO 2009 Nov Silver 1.A Coin Game 动态规划
- USACO 2010 Open Silver 3.Time Travel 链表?
- 【poj 3268】 Silver Cow Party 【USACO 2007 February Silver】
- bzoj 3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(SG函数)
- USACO 2009 Mar Silver 3.Look Up
- poj 3279 Filptile (USACO 2007 Open Silver)
- USACO 2016 open Silver 解题报告
- 【USACO 2007 November Silver】Cow Hurdles奶牛跨栏
- Silver Cow Party(USACO 07 FEB & POJ3268)
- bzoj3298 [USACO 2011Open]cow checkers
- USACO 2011 Open Silver 1.Corn Maze (bfs)
- USACO 2015 US OPEN CONTEST,Silver Division Solution
- [USACO 2007 Open Silver] City Horizon - 离散化+线段树
- 【USACO 2007 Open Silver】City Horizon 线段树
- 浅谈Java中的深拷贝和浅拷贝
- 36个Android开发常用代码片段
- Loadrunner:集合点(Rendezvous)
- TCP阻塞控制详解
- Bmob数据库查询结果为null
- USACO 2009 Open Silver 3.Cow Digit Game简单博弈论
- Swing超基础学习总结——4、CardLayout与ActionListener
- Android静默安装实现方案,仿360手机助手秒装和智能安装功能
- mysql主从复制
- 工作线程中弹出Toast
- 技术之外
- leetcode :Binary Search:Sqrt(x)(069)
- consul 配置---K/V存储及ACL
- runtime