hdu 1846 Brave Game (巴什博奕)

hdu 1846 题目链接

题意:有n个石子,每次最多取m个,最少取1个,如果没有石子可取就输了。给出n,m,两个人都很聪明,问先手和后手谁赢。。

思路:

首先定义几个概念:

p点:即必败点,某玩家位于此点,只要对方无失误,则必败

**  N点 :即必胜点,某玩家位于此点,只要自己无失误,则必胜。**

**    一、 所有终结点都是必败点P(这道题目中,轮到谁取石子,还剩0个石子的时候,此人无石子可取,就输了);**

**    二、所有一步能走到必败点P的就是N点;(这里是_存在一种_情况可以走到p点即可)**

**    三、通过一步操作只能到N点的就是P点; (这里是_所有_的情况都只能走到n点)**

那么当m=3的时候,则有:

  x  :0   1  2   3  4    5  6  7  8   9  10…

pos:P   N  N  N  P   N  N  N  P  N   N …

1,2,3为n点是因为一步可以走到p点0,直观得说就是剩余1,2,3个石子的时候可以一次拿走。

4为p点是因为,不管怎么走,下一步一定是处于1,2,3这三个n点的,因此4是p点。

因此我们可以得出结论:n%(m+1)==0的时候,后手赢,否则先手赢。

(之前遇到的时候只记了结论,不清楚为什么,这下明白了orz

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月19日 星期二 19时41分16秒
 4File Name :code/hdu/1846.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int n,m;
34int main()
35{
36	#ifndef  ONLINE_JUDGE 
37	freopen("code/in.txt","r",stdin);
38  #endif
39	int T;
40	cin>>T;
41	while (T--)
42	{
43	    scanf("%d%d",&n,&m);
44	    if (n%(m+1)==0)
45	    {
46		puts("second");
47	    }
48	    else
49	    {
50		puts("first");
51	    }
52	}
53
54  #ifndef ONLINE_JUDGE  
55  fclose(stdin);
56  #endif
57    return 0;
58}