POJ2505 A multiplication game
A multiplication game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4350 | Accepted: 2147 |
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.
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
思路:该题为博弈论方面的题目。给定一个n,原始数从1开始(令q=1),每次乘以2~9中的任意一个数[q=q*(2~9)],当q>=n时停止操作,最后一个完成此操作的人为 胜利者。
由于每个人都是用最好的策略,所以关键是找出必败态和必胜态。在区间[ceil(n/9.0),正无穷大)为必胜态,由这个必胜态可以推出[ceil(ceil(n/9.0)),ceil(n/9.0)-1]为必败态,然后由这个必败态又可以推出之前的必胜态,由必胜态->必败态,......最后推到当区间内包含1时停止。判断总共所退的步骤数。从而可以得出结论。
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <map> 7 #include <string> 8 9 #define MAXINT 99999999 10 11 using namespace std; 12 13 14 15 int main(int argc, char *argv[]) 16 { 17 int i,j,k; 18 int n; 19 20 21 22 while(scanf("%d",&n)!=EOF) 23 { 24 25 26 for(i=0;;i++) 27 { 28 if(i%2==0) 29 n=ceil(n/9.0); 30 else 31 n=ceil(n/2.0); 32 33 if(n<=1) 34 break; 35 } 36 37 if(i%2==0) 38 {printf("Stan wins.\n");} 39 else 40 {printf("Ollie wins.\n");} 41 42 43 44 } 45 46 47 48 49 // system("PAUSE"); 50 return EXIT_SUCCESS; 51 }