1. 程式人生 > >HDU1517 Multiply Game

HDU1517 Multiply Game

per () 每次 splay for style star first inpu

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

HDU1517 Multiply Game