[求PN点] poj 2505 A multiplication game

题目链接:

http://poj.org/problem?id=2505


A multiplication game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5098   Accepted: 2573

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

Waterloo local 2001.09.22

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给一个n,两个人轮流玩游戏,从p=1开始,每次可以选择乘以2或者9,谁第一次到达n赢,假设两人都足够聪明。

1 < n < 4294967295

解题思路:

找P必败点,N必赢点

代码解释的很详细:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

int n;

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%d",&n))
   {
       int ans=0; //必输
       int cur=n-1;

       while(cur>=1)
       {
           if(!ans) //后面是必输的状态
               cur/=9;  //在cur/9+1~cur内的人肯定选择*9 获得必胜
           else  //后面是必赢
                cur/=2; //至少要选择*2
          // printf("ans:%d cur:%d\n",ans,cur);
           //system("pause");
           ans^=1;
       }
       if(ans)
            printf("Stan wins.\n");
       else
            printf("Ollie wins.\n");

   }
   return 0;
}

[求PN点] poj 2505 A multiplication game,布布扣,bubuko.com

时间: 2024-12-15 01:35:33

[求PN点] poj 2505 A multiplication game的相关文章

poj 2505 A multiplication game

A multiplication game POJ - 2505 题目大意:两个人轮流玩游戏,Stan先手,数字 p从1开始,Stan乘以一个2-9的数,然后Ollie再乘以一个2-9的数,直到谁先将p乘到p>=n时那个人就赢了,而且轮到某人时,某人必须乘以2-9的一个数. /* 博弈,找规律,首先我们容易得到在 [2,9]这个区间,是Stan必胜 [10,18]这个区间,是Ollie必胜 那么下个区间左边肯定是[18+1,?],是Stan必胜 区间闭应该填一个什么数呢,因为18是一个必败点,所

poj 2505 A multiplication game(博弈)

A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5622   Accepted: 2811 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 =

[原博客] POJ 2505 A multiplication game 组合游戏

题目链接题意: 有一个数p=1,甲乙两人轮流操作,每次可以把p乘2~9中的一个数,给定一个n,当一个人操作后p>=n,那么这个人赢,问先手是否必胜. 必胜状态:存在一种走法走到一个必败状态. 必败状态:后继状态都为必胜状态. 我们可以知道>=n的数都为必败状态,可以转移到>=n的最小的数为n/9(上取整),所以 n/9~n-1都为必胜态,同理n/9/2(都为上取整)为最小的必须转移到n/9~n-1(必胜状态)的状态,所以n/9/2~n/9-1为必败态,于是就可以这样推到1,看一下1是必胜

POJ 2505 A multiplication game(找规律博弈/贪心)

题目链接 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; int main() { ll n; while(~scanf("%I64d",&n)) {//其实算是 贪心了吧 //先手想赢,他会x2,这样子才能尽量避免让后手赢 //后手想赢,他就会x9,只有乘最大的,他胜算才最大 int t=0; ll l=1,r=9; while(1) { if

poj 2505 A multiplication game(博弈)

还的确是一个稍有难度博弈的问题(这个可不属于博弈中的任何一个): 题意:游戏规则为:两个人在2-9选数选出之后与p相乘,此时p=p*(2...9);当p>=n时这一方获胜.. 分析: 如果输入是 2 ~ 9 ,(2~9)因为Stan 是先手,所以Stan 必胜 如果输入是 10~18 ,(9+1~9*2)因为Ollie 是后手,不管第一次Stan 乘的是什么,Stan肯定在 2 ~ 9 之间,如果Stan乘以 2 ,那么Ollie就乘以 9 ,就到18了,如果Stan乘以 9 ,那么Ollie乘

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit