*【HDU - 1517】【POJ - 2505】A Multiplication Game(博弈,递推找规律或SG函数)
題干:
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 34012226Sample Output
Stan wins. Ollie wins. Stan wins.解題報告:
? ?SG函數的方法目前還不會?
一個題解:
這里用一種推導的方法來進行求解分析如下:Stan選2-9之間的數如果是2,那么Ollie選的范圍只能在4-18,這樣Stan控制了19 - 4*9;如果是3,那么Ollie選的范圍只能在6-27,這樣Stan控制了28 - 6*9;如果是4,那么Ollie選的范圍只能在8-36,這樣Stan控制了37 - 8*9;如果是5,那么Ollie選的范圍只能在10-45,這樣Stan控制了46 - 10*9;如果是6,那么Ollie選的范圍只能在12-54,這樣Stan控制了55 - 12*9;如果是7,那么Ollie選的范圍只能在14-63,這樣Stan控制了64 - 14*9; 如果是8,那么Ollie選的范圍只能在16-72,這樣Stan控制了73 - 16*9; 如果是9,那么Ollie選的范圍只能在18-81,這樣Stan控制了82 - 18*9;===============綜上:范圍在2 – 9 是Stan win!(2-9)范圍在9+1 – 9*2 是Ollie win!(10-18)范圍在9*2+1 –9*2*9 是Stan win!(19-162)范圍在9*2*9+1 –9*2*9*2 是Ollie win!范圍在9*2*9*2+1 –9*2*9*2*9 是Stan win!........================相信大家看得出規律了吧,范圍左側是上一個范圍的右側加一,右側是上一個右側交替的乘以2和9;AC代碼:
#include<cstdio> #define ll long long using namespace std; ll n; int main() {while(~scanf("%lld",&n)) {ll cur = 1;bool flag = 0;//=1代表先手 while(cur<n) {//注意162這種邊界,所以不能cur<=nif(!flag) {flag=1;cur*=9;}else {flag=0;cur*=2;}}if(flag) puts("Stan wins.");else puts("Ollie wins.");}return 0 ; }AC代碼2:
題解
#include <stdio.h> int main() {double n;while(~scanf("%lf",&n)){while(n>18)n/=18;if(n>=1&&n<=9)printf("Stan wins.\n");elseprintf("Ollie wins.\n");}return 0; }?
總結
以上是生活随笔為你收集整理的*【HDU - 1517】【POJ - 2505】A Multiplication Game(博弈,递推找规律或SG函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【CodeForces - 155C】H
- 下一篇: 《全境封锁2》国服首测将至:显卡最低GT