BNUOJ1A. 取石子游戏(博弈)

题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6390#problem/1A

 

解题思路:

    博弈论经典题目的变形。我们可以假设已经到了最后决定输赢的两轮,最优的情况就是当前仅剩m+1个石子,然后当前的对手最多拿走m个,这样可以保证(轮到我)最后一个人至少有一个石子可拿,并且保证一次拿光。

    总结上面的规律,两个人都使用最优策略。他们每个人都想做(我)最后一个人,那么在每轮取石子的时候,都会想尽办法让剩下的石子为(m+1)的倍数。这里我们还要注意,每次最多取m个,不可以多取哦!

    至于判断最后赢的人是谁,开一个flag记录下就行。

 

完整代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>
#include <stack>
#include <time.h>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 6000001;

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int T;
    cin >> T;
    while(T--)
    {
        int n , m;
        cin >> n >>  m;
        int flag = 0;
        while(n > 0)
        {
            int cnt = 1;
            while((n - cnt) % (m + 1) != 0 && cnt < m)
                cnt ++;
            n -= cnt;
            if(flag == 0)   flag = 1;
            else    flag = 0;
        }
        if(flag == 0) cout << "second" << endl;
        else cout << "first" << endl;
    }
    return 0;
}


/*************************************************
*
*   Copyright By DoubleQ
*   Written in 2015
*   Blog Address : zhanghe.ac.cn
*                  http://blog.csdn.net/u013447865
*   Email Address: acmer_doubleq@qq.com
*
*************************************************/

 

posted on 2015-07-14 21:04  DoubleQ  阅读(268)  评论(0)    收藏  举报

导航