Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8313   Accepted: 3374

Description

Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):

         25 7

11 7

4 7

4 3

1 3

1 0

an Stan wins.

Input

The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.

Output

For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.

Sample Input

34 12
15 24
0 0

Sample Output

Stan wins
Ollie wins

Source

 

SolutionⅠ  

黄金比例
如果两个数相等,或者两数之比大于斐 波拉契数列相邻两项之比的极限((sqrt(5)+1)/2),则先手胜,否则后手胜。

var a,b:longint;f:boolean;

procedure swap();
begin
b:=a xor b;
a:=a xor b;
b:=a xor b;
end; begin
while true do
begin
readln(a,b);
if (a=)and(b=) then halt;
if a>b then swap();
if (a=b)or(b/a>=(sqrt()+)/) then writeln('Stan wins')
else writeln('Ollie wins');
end;
end.

SolutionⅡ

给定两堆石子,二人轮流取子,要求只能从石子数目较大的那一堆取子,取子的数目只能是另一堆石子数目的倍数.最终使得某一堆数目为零的一方为胜.

首先,容易看出,对于每一个局面,要么是先手必胜,要么是后手必胜,最终结果完全由当前局面完全确定.

另外,可以简单罗列一下先手必胜和必败的几种局面(两堆石子初始数目都大于零):

1,有一堆石子数目为一,先手必胜,  1,4,    1,2.
2,两堆石子数目差一,且两堆石子数目都不为一,先手必败(只能使后手面对必胜的局面),如  3,4  5,6   .
3,如果数目较大的那一堆是数目较小那一堆的2倍加减一,且不是上面两种局面,先手必胜,2,5  3,5  3,7.

可是上面这些信息对于解决这个问题还是有一些困难.

再进一步试算数目较小的石子,可以发现,当两堆数目相差较大时,总是先手必胜.
事实上,进一步探讨可以发现下面的结论:

1,N<2*M-1时,先手别无选择,只能使之变为 N-M,M 局面,(易见)如3,5  5,7  7,4...

2,设两堆石子数目为N,M(N>M>0,且N,M互质),则若N>=2*M-1,且N - M ! =1时,先手必胜.要求M,N互质是因为对于M,N有公因数的情形,可以同时除以其公因数而不影响结果.

简单说明一下上面结论2的由来. N>=2*M-1时,先手可使之变为  N%M,M  或N%M+M,M两种局面之一,其中有且只有一个必败局面。注意到如果N%M,M不是必败局面,那么N%M+M,M就是必败局面,因为面对N%M+M,M这个局面,你别无选择,只能在前一堆中取M个使对方面对必胜局面(结论1 )。

解释来源:http://www.cppblog.com/sdz/archive/2010/08/29/125124.html

var a,b:int64;f:boolean;

procedure swap();
begin
b:=a xor b;
a:=a xor b;
b:=a xor b;
end; procedure main;
begin
while = do
begin
if a>b then swap();
if (b mod a=)then break;
if (b-a>a) then break;
dec(b,a);
if f=false then f:=true;
if f=true then f:=false;
end;
if f then writeln('Stan wins')
else writeln('Ollie wins');
end; begin
while true do
begin
readln(a,b);
f:=true;
if (a=)and(b=) then halt;
main;
end;
end.

[POJ2348]Euclid's Game的更多相关文章

  1. [poj2348]Euclid's Game(博弈论+gcd)

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9033   Accepted: 3695 Des ...

  2. 【博弈论】poj2348 Euclid's Game

    假设当前b>a. 一.b%a==0 必胜 二.b<2*a,当前我们没有选择的余地,若下一步是必胜(最终能到情况一),则当前必败:反之,当前必胜. 三.b>2*a,假设x是使得b-ax ...

  3. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

  4. 博弈论BOSS

    基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_c ...

  5. 【Mark】博弈类题目小结(HDU,POJ,ZOJ)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小 ...

  6. POJ 2348 Euclid's Game(博弈)题解

    题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败 思路:这题用博弈NP思想,必败点和必胜点之间 ...

  7. ZOJ1913 Euclid's Game (第一道简单的博弈题)

    题目描述: Euclid's Game Time Limit: 2 Seconds      Memory Limit: 65536 KB Two players, Stan and Ollie, p ...

  8. Euclid求最大公约数

    Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...

  9. HDU 1525 Euclid's Game 博弈

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 电脑升级完Xcode8后 注释快捷键无效的问题

    1.部分电脑升级完Xcode8 后直接重启电脑就可以使用Command +/ 快捷键注释代码, 2.如果上述方法没有效果,可以在终端输入sudo /usr/libexec/xpccachectl  然 ...

  2. linux磁盘设备知识

    linux分区数字编号: 1.分区数字编号1至4留给主分区或扩展分区使用,逻辑分区编号从5开始. 2.IDE硬盘设备名均以/dev/hd开头,不同硬盘编号依次是/dev/hda/./dev/hdb./ ...

  3. 4、Hbase

    1).逻辑模型 Hbase 以表的形式存储数据,每个表由行和列组成,每个列属于一个特定的列族. 表中由行和列确定的存储单元称为一个元素,每个元素保存了同一份数据的多个版本,由时间戳来标识.行健是数据行 ...

  4. 微软Hololens学院教程- Holograms 101: Introduction with Device【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  5. 软媒魔方u盘装系统

    http://jingyan.baidu.com/article/d5a880eb6e747e13f147ccd6.html

  6. map(function, sequence)

    map(function, sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回: >>> lt = range( ...

  7. javaweb学习总结(三十六)——使用JDBC进行批处理

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...

  8. 奇怪的JS

    有的时候发现JS是一门很高深的语言,不是我等俗人可以学会,没有private,没有public不说,居然连Class都没有,这个世界就是这样,有的东西你不一定非要想通,也不一定非要剖根问底,有的时候你 ...

  9. eclipse 新建项目下后.metadata\.plugins的文件夹解释和如何保存自己的特定工程设置

    eclipse 新建项目下后.metadata\.plugins的文件夹解释和如何保存自己的特定工程设置 [org.eclipse.core.runtime] 字体,maven的setting.xml ...

  10. bzoj1056

    花了一上午大概复习了一下splay,treap 像这种裸的数据结构题在js应该会越来越少 不过练练手也好, 这就是平衡树+hash,如果这是单纯的BST应用,还是写treap吧,好调试 ;       ...