HDU1517 巴什博弈变形

来源:互联网 发布:华为数据丢失事件 编辑:程序博客网 时间:2024/05/16 08:19

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.

这个题是我第一次看到博弈问题,先百度了巴什博弈,从那个思想中想出来的这个方法。
这类题首先可以从个例开始
看到Stan是先手,那么在n在2到9的时候S必胜
其次分析10往后的,S不想让O赢,但是S也赢不了,因为是聪明的所以题干中S会采用最小的2乘进去。而O想赢,他就用最大的去乘。所以再10到18的时候O是必胜的。

从第三个例子19往后的情况和钱面两种思想不是非常一样,我在这里卡了一会。因为从19开始,S和O都要考虑一下自己是不是能一次成功或者是为了下一次的成功做铺垫(可能是我想多了)从19开始举例的话,首先S要保证O不能超过18,因此S只能是2,而O看到自己也赢不了,会尽量出2,那么S就会出9来保证尽量赢,2*2*9=36,也就是说在19的36之间赢得会是S。
那么如果是37会怎样呢。
如果S为了让自己赢,一定会出限制内最大的数,也就是4,再大的话O就赢了。然后到O出数,O为了不让S轻易到达37,就一定会出最小的那个数,也就是2。再一轮轮到S出的时候肯定又是9,这样可以算出是4*2*9=72。也就是说在37到72之间赢得还是S。从19和37之间我们可以考虑出来一个规律。在控制力在先手的时候,赢得就是先手。因此我们不妨直接扩大这个结果。S能控制的最大数是9,那么三部之内9*2*9=162之前,18之后的数赢得全部都是S。那么这道理就讲得通了。

推广到一般的话
n winner
0到9 9 S
10到18 2* 9 O
19到162 9*2*9 S

这样算下去就能轻松的找到了一般规律
最后贴AC代码

 #include<iostream>using namespace std;int main(){    long long n;    while (cin >> n)    {        long long ant = 1;        long long sum;        for (int a = 1;;a++)        {            if (a % 2 == 0)            {                ant = ant * 2;            }            else            {                ant = ant * 9;            }            if (ant >= n)            {                sum = a;                break;            }        }        if (sum % 2 == 0)        {            cout << "Ollie wins." << endl;        }        else        {            cout << "Stan wins." << endl;        }    }    return 0;}
0 0
原创粉丝点击