zoukankan      html  css  js  c++  java
  • hdu 1525 Euclid's Game

    Euclid's Game

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


    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 12
    15 24
    0 0
    Sample Output
    Stan wins
    Ollie wins
    如果 a,b之间是倍数关系,那么第一个一定赢
    假设 a<b,经过一系列的操作后,(a,b)一定会变成 (a,b-b/a*a)       //b/a是整除
    如果b/a=1,就没有主动权,因为只能选择减掉一个a
    如果b/a>1, 就掌握了主动权:可以之间选择减掉b/a*a,让后面的人面对(a,b-b/a*a),也可以减掉(b/a-1)*a,后面的人就必须减掉一个a,让自己面对(a,b-b/a*a);
    所以只要看谁先掌握主动权,谁就能赢(如果有主动权的话,即b/a>1)
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<vector>
     5 #include<queue>
     6 #include<stack>
     7 #include<algorithm>
     8 #include<cstring>
     9 #include<stdlib.h>
    10 using namespace std;
    11 int p[101000],cnt;
    12 int main(){
    13     int n,m;
    14     while(cin>>n>>m,n+m){
    15         if(n>m) swap(n,m);
    16         if(m%n==0){
    17             cout<<"Stan wins"<<endl;continue;
    18         }
    19         cnt=0;
    20         while(m%n!=0){
    21             p[cnt++]=m/n;
    22             m-=m/n*n;
    23             if(n>m) swap(n,m);
    24         }
    25         int can=0,ps=-1;
    26         for(int i=0;i<cnt;i++)
    27           if(p[i]>1){
    28             ps=i;break;
    29           }
    30         if(ps==-1) {
    31             if(cnt%2) cout<<"Ollie wins"<<endl;
    32             else cout<<"Stan wins"<<endl;
    33         }
    34         else{
    35             if(ps%2) cout<<"Ollie wins"<<endl;  //第二个首先掌握主动权
    36             else cout<<"Stan wins"<<endl;
    37         }
    38     }
    39 }
     
  • 相关阅读:
    JS事件冒泡
    iis设置Gzip后,无后缀的url无法压缩解决 MVC iis GZIP
    SQL中的循环、for循环、游标
    采用CDN加速后,如何在程序里获取用户IP地址
    Resharper 的快捷键
    JS防后退跳转
    ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
    Log4net创建日志及简单扩展
    统治世界的十大算法
    ffmpeg save rtsp stream
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3931892.html
Copyright © 2011-2022 走看看