码迷,mamicode.com
首页 > 其他好文 > 详细

组合游戏简单题

时间:2015-08-08 16:40:03      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:acm   uva   组合游戏   博弈   



UVA - 11859 - Division Game

题目传送:Division Game

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 10005;

int n, m, t;

int match[55];//把一行的若干个数都变为他们的真因子,即拿掉了一些素因子,即可以看成一堆火柴中取出几根来,所以可以看做Nim类组合游戏

int chu[maxn];
int to[maxn];//计算出每个数字的素因子个数

void init() {//打表计算素因子
    to[1] = 0;
    for(int i = 2; i < maxn; i ++) chu[i] = i;

    for(int i = 2; i < maxn; i ++) {
        if(to[i]) continue;
        to[i] ++;
        for(int j = 2 * i; j < maxn; j += i) {
            while(chu[j] % i == 0) {
                to[j] ++;
                chu[j] /= i;
            }
        }
    }
}


int main() {
    init();
    int T;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas ++) {

        memset(match, 0, sizeof(match));

        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                scanf("%d", &t);
                match[i] += to[t];
            }
        }

        //因为题目可以看做Nim游戏,所以sg数值即为火柴数值的异或和
        int sg = match[1];
        for(int i = 2; i <= n; i ++) {
            sg = sg ^ match[i];
        }

        if(sg == 0) printf("Case #%d: NO\n", cas);//sg为0时先手必败,反之必胜
        else printf("Case #%d: YES\n", cas);

    }
    return 0;
}



UVALive - 5059 - Playing With Stones

题目传送:Playing With Stones

此题可以先打表来找规律。

打表代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 100;
int SG[maxn];
int vis[maxn];

int main() {
    SG[1] = 0;//只有一个的时候不能拿,为必败状态
    for(int i = 2; i <= 30; i ++) {
        memset(vis, 0, sizeof(vis));
        for(int j = 1; j * 2 <= i; j ++) vis[SG[i-j]] = 1;
        for(int j = 0;; j++) if(!vis[j]) {
            SG[i]  = j;
            break;
        }
        printf("%d ", SG[i]);
    }
    return 0;
}

规律为:
n为偶数时,SG(n) = n / 2;
n为奇数时,SG(n) = SG(n/2);

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;

LL SG(LL x) {
    return (x & 1) ? SG(x / 2) : x / 2;
}

int main() {
    int T;
    cin >> T;
    while(T --) {
        int n;
        LL a, ans = 0;
        cin >> n;
        for(int i = 0; i < n; i ++) {
            cin >> a;
            ans ^= SG(a);
        }   

        if(ans) cout << "YES\n";
        else cout << "NO\n";

    }   
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

组合游戏简单题

标签:acm   uva   组合游戏   博弈   

原文地址:http://blog.csdn.net/u014355480/article/details/47359113

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!