HDU 3951 Coin Game 博弈论

来源:互联网 发布:程序员常用画图工具 编辑:程序博客网 时间:2024/06/05 09:16

题目大意: 给你n个硬币围成一圈, 两人轮流拿连续的硬币, 拿的个数不能大于k个, 另外只能拿连续的,中间有间隔就不能拿。

这题其实挑战上有一道类似的,,比赛的时候想了个思路感觉可以然后写了写一不小心就过了Orz, 首先说特殊情况, k = 1 的时候只能一人一次拿一个也就是说输赢看奇偶, 然后n <= k的时候先手可以一次性拿完, 重要的就是 n > k 的时候后手必胜这个当时真真是不敢交。。思路其实挺好理解, 先手拿了几颗后, 后手在对面拿, 让剩下的硬币分成数量一样的两部分, 这样的话就形成了nim游戏中的平衡态, 即不管先手怎么拿, 后手在另一堆进行一样的操作, 这样后手可以保证必胜。

代码非常简单:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cctype>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define ls id<<1,l,mid
#define rs id<<1|1,mid+1,r
#define OFF(x) memset(x,-1,sizeof x)
#define CLR(x) memset(x,0,sizeof x)
#define MEM(x) memset(x,0x3f,sizeof x)
typedef long long ll ;
typedef pair<int,int> pii ;
const int maxn = 1e5+50 ;
const int inf = 0x3f3f3f3f ;
const int MOD = 1e9+7 ;


int main () {
#ifdef LOCAL
freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//      freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
#endif
    int T;
    scanf("%d", &T);
    int cas = 1;
    while (T--) {
        int a,k;
        cin >> a >> k;
        bool flag;
        if (k == 1) flag = a & 1;
        else if (a <= k) flag = 1;
        else flag = 0;
        if (flag) printf("Case %d: first\n",cas++);
        else printf("Case %d: second\n", cas++);


    }
return 0;
}

0 0
原创粉丝点击