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 }
     
  • 相关阅读:
    script 标签的defer,async的作用,及拓展浏览器多线程,DOMContentLoaded
    vuex基本熟悉与使用
    关于h5屏幕适配
    react-router4.0的使用
    使用gulp 合并压缩打包,实时监控文件,实现本地server
    组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)
    vue怎么样创建组件呢??
    基于ionic框架封装一个图片轮播指令的几点
    使用php在服务器端生成图文验证码(二)
    字符串与对象的相互转化
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3931892.html
Copyright © 2011-2022 走看看