HDU 1525 Euclid's Game (博弈)
Posted 爱国呐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1525 Euclid's Game (博弈)相关的知识,希望对你有一定的参考价值。
HDU 1525 Euclid‘s Game (博弈)
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.
Examples
Sample Input
34 12 15 24 0 0 Sample Output
Stan wins Ollie wins
题意:
题目给出了两个正数a.b
每次操作,大的数减掉小的数的整数倍。一个数变为0 的时候结束。
谁先先把其中一个数减为0的获胜。问谁可以赢。Stan是先手。
思路:
假设一个这样的状态T1=(x,y),并且x>y,
那么状态T2=(x+y,y)可以到达T1,并且两者之间一个为必胜状态,那么另外一个为必败状态。
但是,T3=(x+2y,y)既可以到达T1状态,又可以到达T2状态,那么我们可以说T3状态是一个必胜状态,
同理T4=(x+3y,y),...(x+4y,y)等几个状态,也同样可以到达T1与T2状态,那么这些状态也为必胜状态
所以我们说如果满足x>=2y,则为必胜状态
如果是y<x<2y,这种情况,下个状态只能为(x-y,y),为了保证前面一个数大于后面一个数,我们会将两者交换
x不可能小于y,因为前提假设了x>y
如果x==y,或者x=0或者y=0,那么当前取石子的人胜利
代码:
#include<stdio.h> #include<math.h> #include<string.h> int main() { int a,b; while(1) { int i; scanf("%d%d",&a,&b); if(a==0||b==0) break; for(i=0;;i++) { if(a<=b) { if(b%a==0||(b-a>=a))break;//这里最好不要用b>=2*a,因为2*a可能会超过int,也可以改为long long用b>=2*a; else b-=a; } else { if(a%b==0||(a-b>=b)) break; else a-=b; } } if(i%2==0) printf("Stan wins\n"); else printf("Ollie wins\n"); } }
以上是关于HDU 1525 Euclid's Game (博弈)的主要内容,如果未能解决你的问题,请参考以下文章