http://acm.nyist.net/JudgeOnline/problem.php?pid=401&&简单的博弈论

After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.



The game goes like this:
Two players start the game with a circle of n coins.
They take coins from the circle in turn and every time they could take 1~K continuous coins.
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin wins the game.
Suppose that those two players always take the best moves and never make mistakes.
Your job is to find out who will definitely win the game.
输入
The first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=109,1<=K<=10).
输出
For each case, output the number of case and the winner "first" or "second".(as shown in the sample output)
样例输入
2
3 1
3 2样例输出
Case 1: first

Case 2: second

这是一道我们学校对外赛的月赛题,比赛时本来我是要做对外的题,听zmh童鞋说,选的大部分都是oj的老题,,于是就没有做对外赛了,直接做了队内的题,结果这道题愣是木做,然后在比赛后我看了这道题,却不知怎么做,不是木思路而是我看见关于数学方面的题我就不想弄,大概是我数学学得不好的原因吧。下面说一下这道题的思路,,,我认为这和我们oj的取石子一挺像的,在这里是把石子连接成环。首先第一直觉感觉后取赢得可能性大些,但凭直觉是不行的,下面简单说一下吧,对于先取的而言,只要取一次,石子环就变成石子链,而后取者只要每次在先取者对称的地方取相同的就行,这里只要先取者有的取,后取者也一定有的取。所以说大部分后取的赢。什么时候先取的赢呢,有两种情况,一是总的石子小于一次取的,其次是最多取连续的一个,并且石子的个数为奇数。

AC代码:

#include<iostream>
#include<string.h>
using namespace std;
int main()
{   int  Case;
    cin>>Case;
    for(int i=1;i<=Case;++i)
    {   int n,m;
         cin>>n>>m;
         if(m>=n||(m==1&&n%2==1)) 
           cout<<"Case "<<i<<": first" <<endl;
           else  cout<<"Case "<<i<<": second" <<endl;
           }return 0;
    }

下面也顺便说一下取石子一把,就是如果可以一次性取完则先取者赢,下面一种情况就是,看总石子数对(k+1)取余是不是为零,如果为零,则后取者赢,否者先取者赢。

这里k为一次最多取的个数。

AC代码:

 
#include<stdio.h>
main()
{
	int n,m,p;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&m,&p);
     if(p>=m)
      printf("Win\n");
     else
	 {
     	if(m%(p+1)==0)
	    	printf("Lose\n");
	    else printf("Win\n");
	 }
	}
}
        


你可能感兴趣的:(http://acm.nyist.net/JudgeOnline/problem.php?pid=401&&简单的博弈论)