zoukankan      html  css  js  c++  java
  • Euclid's Game (博弈论)

    Euclid's Game

     HDU - 1525 

    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. 

    InputThe input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.OutputFor 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

    题意:给出两个数,a和b,将大的数中,减去若干b的倍数,最终有一个数为0的话就胜了。
    题解:假设a>=b,那么如果a==b,先手必胜,如果a%b==0,先手必胜。因为如果b<a<2*b的话,怎么取就已经定了,所以如果a>2*b,那么先手可以决定谁先取到b<a<2*b这个状态,所以如果a>2*b,先手必胜,只用讨论当b<a<2*b时最后谁胜。
     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int n,m;
     5 int main()
     6 {
     7     while(~scanf("%d%d",&n,&m))
     8     {
     9         if(n+m==0)
    10             break;
    11         if(n<m)
    12             swap(n,m);
    13         if(n%m==0)
    14         {
    15             printf("Stan wins
    ");
    16             continue;
    17         }
    18         bool flag=true;
    19         while(1)
    20         {
    21             if(m==0||n%m==0||n/m>=2)
    22                 break;
    23             int t=n;
    24             n=m;
    25             m=t-n;
    26             flag=!flag;
    27 
    28         }
    29         if(flag)
    30             printf("Stan wins
    ");
    31         else
    32             printf("Ollie wins
    ");
    33     }
    34 }
  • 相关阅读:
    Java技术路线--2循环
    Java技术路线--1基本类型与包装类
    Linux内存管理之UMA模型和NUMA模型
    最长XX子串/数组/子序列
    epoll LT 模式和 ET 模式详解
    OS篇:OS中进程的阻塞与挂起的区别
    约瑟夫环问题
    最大公约数和最小公倍数
    C++之寻找素数(素数筛)
    Linux OOM机制分析
  • 原文地址:https://www.cnblogs.com/1013star/p/9700572.html
Copyright © 2011-2022 走看看