zoukankan      html  css  js  c++  java
  • Day11

    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

    思路:一开始考虑PN状态是否跟奇偶有关,举出反例后pass,那么就找规律,x 0的状态是P状态,可以确定N状态为 x y (y%x=0),那么当y>=2*x时就可以判断状态,y==2*x时,一定是N状态,y>2*x时,先手可以判断y%x x是否为必败态,若y%x x为必败态,先手就将该状态转移给后手,否则,先手就将y%x+x x转移给后手,后手一定只能把y%x x转移给先手,先手必胜,也就是说,当y>=2*x时,先手就掌握了比赛,是必胜态
    #include<bits/stdc++.h>
    using namespace std;
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    
    
    void run_case() {
        int n, m;
        while(cin >> n >> m && n + m) {
            int win = 1;
            if(n < m) swap(n, m);
            while(m) {
                if(n%m == 0 || (n / m) > 1) break;
                n -= m;
                swap(n, m);
                win ^= 1;
            }
            if(win) cout << "Stan wins
    ";
            else cout << "Ollie wins
    ";
        }
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        //int t; cin >> t;
        //while(t--)
        run_case();
        cout.flush();
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    Window中的Docker 拉取Mysql镜像 并在本地Navicate链接
    Error response from daemon:###unable to delete ### (must be forced)
    WIN10中DOCKER的安装
    ASP.NET MVC3调用分部视图-PartialView的几种方式(集)
    jQuery select操作控制方法小结
    前置体验,才是打动用户的神器
    ASP.NET MVC form验证
    ASP.NET MVC 3 Model【通过一简单实例一步一步的介绍】
    .Net Mvc3框架调用服务端控件解决方案
    Razor基础语法简介
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12312301.html
Copyright © 2011-2022 走看看