hdu 1525 Euclid's Game 博弈

来源:互联网 发布:南京江北新区网络问政 编辑:程序博客网 时间:2024/05/23 15:06

Euclid's Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2591    Accepted Submission(s): 1170


Problem 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 1215 240 0
 

Sample Output
Stan winsOllie wins
 

Source
University of Waterloo Local Contest 2002.09.28
 

由于考虑到之前做的一些题,忽略了打表找规律的重要性,做这个题时,先打了一张规律表,试图直接推出公式,

结果花了很长时间无果。后来觉得似乎不能直接得出公式,但是对于每种情形的处理方式可以得出。

对于两个数a,b 

令a<=b

如果b>=2*a,先手必胜

如果b==a,先手必胜

否则转化为问题(b-a,a)的反结果

(打表找真理)



/**========================================== *   This is a solution for ACM/ICPC problem * *   @source£ºhdu 1525 *   @type: *   @author: wust_ysk *   @blog:  http://blog.csdn.net/yskyskyer123 *   @email: 2530094312@qq.com *===========================================*/#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;bool work(ll a,ll b){    if(b>=2*a) return true;    if(b==a)  return true;    return !work(b-a,a);}int main(){   ll a,b;   while(~scanf("%lld%lld",&a,&b)&&(a||b))   {       if(a>b)  swap(a,b);       puts(work(a,b)?"Stan wins":"Ollie wins");   }   return 0;}


0 0
原创粉丝点击