HDU 1517 A Multiplication Game 博弈论+找规律

来源:互联网 发布:网络新技术论文 编辑:程序博客网 时间:2024/05/22 17:36
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
1621734012226
Sample Output
Stan wins.
Ollie wins.
Stan wins.








传送门
题意:从1开始,每次乘2~9中的任意一个整数,假如超过或者等于n了就赢,
问最优策略下先手是否必胜。

打表找规律可以发现,先手必败的n有:
(9+1)~18
(162+1)~324
……
可以再打几个表,
然后就可以发现规律,先设一个L=9,R=18,
每次判断n是否在(L+1)~R的范围内,如果在的话就必败;
不然L=R*9,R=L*2,然后继续判断直到L+1>n为止。

……似乎还有更简洁的规律?




#include<bits/stdc++.h>using namespace std;int main(){int x,L,R;while (~scanf("%d",&x)){L=9,R=18;bool fl=1;while (L+1<=x){if (R>=x){fl=0;break;} else L=R*9,R=L<<1;}if (fl) puts("Stan wins.");else puts("Ollie wins.");}return 0;}



阅读全文
0 0