A Multiplication Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3691 Accepted Submission(s): 2097
Problem Description
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.
Input
Each line of input contains one integer number n.
Output
For 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.
Source
University of Waterloo Local Contest 2001.09.22
Recommend
LL
我是通过找规律得出来的
因为起始的p=1,当2<=n<=9时Stan赢。如果 n 不在2~9之间的话,Ollie要赢,那么n要不大于能得到的最小值 即10<=n<=18
如果n>18呢,那就分析一下
如果S第一次取2,那么可以得到O能选的区间是[4,2*9],可以得到接下来S能赢得区间是[19,4*9]
如果S第一次取3,那么可以得到O能选的区间是[6,3*9],可以得到接下来S能赢得区间是[28,6*9]
如果S第一次取4,那么可以得到O能选的区间是[8,4*9],可以得到接下来S能赢得区间是[37,8*9]
如果S第一次取5,那么可以得到O能选的区间是[10,5*9],可以得到接下来S能赢得区间是[46,10*9]
如果S第一次取6,那么可以得到O能选的区间是[12,6*9],可以得到接下来S能赢得区间是[55,12*9]
如果S第一次取7,那么可以得到O能选的区间是[14,7*9],可以得到接下来S能赢得区间是[64,14*9]
如果S第一次取8,那么可以得到O能选的区间是[16,8*9],可以得到接下来S能赢得区间是[73,16*9]
如果S第一次取9,那么可以得到O能选的区间是[18,9*9],可以得到接下来S能赢得区间是[82,18*9]
那么就能得到S能赢得区间是[19,162]
所以
[2,9] S win
[9+1,2*9] O win
[2*9+1,2*9*9] S win
[2*9*9+1,2*9*9*2] O win
可以看出范围右边交替乘上2和9,左边是上一个区间的 最大值+1
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 __int64 n; 9 while(scanf("%I64d",&n)!=EOF) 10 { 11 int i=2,j=9; 12 while(1) 13 { 14 if(i<=n&&n<=j) 15 { 16 printf("Stan wins.\n"); 17 break; 18 } 19 else 20 { 21 i=j+1; 22 j=j*2; 23 if(i<=n&&n<=j) 24 { 25 printf("Ollie wins.\n"); 26 break; 27 } 28 i=j+1; 29 j=j*9; 30 } 31 } 32 } 33 return 0; 34 }
HDU 1517 A Multiplication Game (博弈&&找规律)