zoukankan      html  css  js  c++  java
  • HDU1517 Multiply Game

    Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p >= n. 

    InputEach line of input contains one integer number n. 
    OutputFor each line of input output one line either 

    Stan wins. 

    or 

    Ollie wins. 

    assuming that both of them play perfectly. 
    Sample Input

    162
    17
    34012226

    Sample Output

    Stan wins.
    Ollie wins.
    Stan wins.
    题解:

    由于每次都是从p=1开始的,所以只要判断每个游戏中1为必败点还是必胜点即可。(以下各式 / 均为取上整)
    依照上面所提到的算法,将终结位置,即[n,无穷]标记为必败点;
    然后将所有一步能到达此必败段的点标记为必胜点,即[n/9,n-1]为必胜点;
    然后将只能到达必胜点的点标记为必败点,即[n/9/2,n/9-1]为必败点;
    重复上面2个步骤,直至可以确定1是必胜点还是必败点。



    参考代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 ll n;
     5 int main()
     6 {
     7     while(~scanf("%lld",&n))
     8     {
     9         ll cnt=1;
    10         for(int a=1;a;++a)
    11         {
    12             if(a&1) cnt=cnt*9ll;
    13             else cnt=cnt*2ll;
    14             if(cnt>=n)
    15             {
    16                 if(a&1) puts("Stan wins.");
    17                 else puts("Ollie wins.");
    18                 break;
    19             }
    20         } 
    21     }
    22     return 0;
    23 }
    View Code
  • 相关阅读:
    C++类的内存结构
    emplace与insert的区别(C++11)
    C/C++宏的奇技淫巧
    编译器对内存的分配
    利用C++实现模块隐藏(R3层断链)
    PCB标准规范
    RTC 总结
    0.96寸OLED显示屏 IIC接口(SSD1306)
    串行通信简介(SPI, IIC, UART)
    人生感悟
  • 原文地址:https://www.cnblogs.com/csushl/p/10331528.html
Copyright © 2011-2022 走看看