题目:http://community.topcoder.com/stat?c=problem_statement&pm=13204&rd=15857

这道题目须要用到博弈论中的经典理论,Sprague–Grundy theorem,以下将相关理论做一个总结:

Impartial Game:公平游戏,两方除了谁先開始游戏外,其余都同样。

Nim:一类经典的Impartial Game,非常多游戏都能够等价于Nim。

Nimber(Grundy numbers):能够理解为标识一个游戏状态的数。在游戏进行过程种,每一个状态都应一个唯一的Nimber。

Sprague-Grundy定理:对一个 Impartial Game 的状态,其等效游戏的 Nimber 数,就等于全部其后继状态 Nimber 数的 Mex 函数值。

Mex:对一个集合S,若G为S的补集,则 Mex{S} = min {G}。

依据 Sprague-Grundy定理,要求当前游戏状态的Nimber数,则须要求出其全部后继状态的Nimbers,然后对这些Nimbers取Mex值。并且当存在多个“子Impartial
Game
”同一时候进行时,这一定理尤事实上用,对每一个“子游戏”状态的Nimber数进行异或操作,就是该状态的Nimber数。

代码例如以下:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) /*************** Program Begin **********************/ class GameOfSegments {
public:
int winner(int N) {
int Nimbers[1001];
Nimbers[0] = Nimbers[1] = 0;
for (int i = 2; i <= N; i++) {
set <int> options;
for (int j = 0; j <= i - 2; j++) {
options.insert(Nimbers[j] ^ Nimbers[i - j - 2]);
}
int r = 0;
while (options.count(r)) {
++r;
}
Nimbers[i] = r;
}
return (Nimbers[N] > 0 ? 1 : 2);
} }; /************** Program End ************************/

參考:

http://www.cnblogs.com/fishball/archive/2013/01/19/2866311.html

http://www.cnblogs.com/hsqdboke/archive/2012/04/20/2459796.html

http://www.cnblogs.com/hsqdboke/archive/2012/04/21/2461034.html

SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber的更多相关文章

  1. SRM 620 D2L3: RandomGraph, dp

    称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...

  2. poj 3537 Crosses and Crosses 博弈论之grundy值

    题意: 给1*n的格子,轮流在上面叉叉,最先画得3个连续叉叉的赢.问先手必胜还是必败. 分析: 求状态的grundy值(也就是sg值),详细怎么求详见代码.为什么这么求要自己想的,仅仅可意会(别人都说 ...

  3. topcoder SRM 624 DIV2 BuildingHeightsEasy

    从大到小遍历一遍,每次取M个元素,然后求得最小的floor即可 int minimum(int M, vector <int> heights) { sort(heights.begin( ...

  4. topcoder SRM 624 DIV2 CostOfDancing

    排个序,求前k个元素和即可 int minimum(int K, vector <int> danceCost) { sort(danceCost.begin(),danceCost.en ...

  5. SRM 624 Building Heights DivI 解读

    几乎相同的一标题.欲了解更多请参阅:http://community.topcoder.com/stat?c=problem_statement&pm=13211&rd=15857 思 ...

  6. SRM 621 D2L3: MixingColors, math

    题目:http://community.topcoder.com/stat? c=problem_statement&pm=10409&rd=15854 利用高斯消元求线性空间的基,也 ...

  7. SRM 622 D2L3: Subsets, math, backtrack

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855 符合条件的集中非1的元素个数是非 ...

  8. TC250专场

    SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...

  9. 博弈论:寻找先手必胜策略——Grundy值

    选修了人工智能课程,老师布置了调研任务:Grundy,开始看了一些资料并没有看懂. 后来找到了一篇文,写的很棒,里面有好多博弈相关的问题与分析,分享出来给大家: http://endless.logd ...

随机推荐

  1. Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件

    前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果:      因为此效果实现的步骤 ...

  2. HBase(一)HBase入门简介

    一 HBase 的起源 HBase 的原型是 Google 的 BigTable 论文,受到了该论文思想的启发,目前作为 Hadoop 的子项目来开发维护,用于支持结构化的数据存储. Apache H ...

  3. Redis(二)CentOS7安装Redis4.0.10与集群搭建

    一 Redis单机安装 1 Redis下载安装 1.1 检查依赖环境(Redis是C语言开发,编译依赖gcc环境) [root@node21 redis-]$ gcc -v -bash: gcc: c ...

  4. Linux mint 17.3系统安装及常用开发办公软件部署

    关于为什么选择linuxmint17.3作为个人办公开发系统的选择说明: 编者按]提起Linux系统,大家可能最先想到的就是 Linux Mint 和 Ubuntu 两个版本了.近来,开源界貌似激进了 ...

  5. shell心得

    向loader.ctl中插入文本

  6. 001 python入门工具准备

    一:准备 1.python官网 https://www.python.org/ 2.使用开发工具Pycharm 3.使用版本 使用2.7.13 4.安装 5.安装pycharm http://www. ...

  7. vue 中 使用 tradingview

    加载页面时初始化方法: mounted 可以在 mounted 方法中调用 methods 的中的方法 使用 data 中的数据时,在每个方法的开始推荐先定义 var that = this 现在还不 ...

  8. 线程池--ThreadPoolExecutor

    线程池的实现原理 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步骤 需要获取全局锁). 2)如果运行的线程等于或多于corePoolSize,则将任务加入 ...

  9. sublime用浏览器打开html文件

    打开Preferences - 「Key Bindings - User」,添加此行: {"keys": ["ctrl+b"],"command&qu ...

  10. 手机html根据手机分辨率网页文字大小自适应

    问题:不同手机型号屏幕尺寸大不相同,导致同样的文字,有的显示一行,有的显示多行. 通过查资料和自己的尝试解决:网页开发习惯的px单位,手机html开发不适用. 源代码如下: <!DOCTYPE ...