题目: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. Nt函数原型头文件

    //转自看雪,可以作为一个头文件使用,方便快捷 1 NTSTATUS NTAPI NtAcceptConnectPort( OUT PHANDLE PortHandle, IN PVOID PortI ...

  2. Struts DynaActionForm example

    The Struts DynaActionForm class is an interesting feature to let you create a form bean dynamically ...

  3. SQL 根据生日和日期计算年龄

    FLOOR(datediff(DY,p.Dob,o.RegisterTime)/365

  4. java内存溢出分析工具

    http://www.cnblogs.com/preftest/archive/2011/12/08/2281322.html java内存溢出分析工具:jmap使用实战 在一次解决系统tomcat老 ...

  5. HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException

    在使用Maven工程管理工具整合SSM框架时,Mybatis使用逆向工程生成的pojo,mapper接口及映射文件,把mapper接口和映射文件放在DAO工程的同一级src/main/java目录下. ...

  6. Ionic Js九:列表操作

    列表是一个应用广泛在几乎所有移动app中的界面元素.ionList 和 ionItem 这两个指令还支持多种多样的交互模式,比如移除其中的某一项,拖动重新排序,滑动编辑等等. <ion-list ...

  7. java技术选型

    综述 JDK版本:JDK8/JDK7 + OracleJDK/OpenJDK Java开发框架:Spring Boot/Spring + Hibernate/MyBatis 前后端分离:单页应用/模板 ...

  8. Linux基础命令—网卡

    #1.实时查看网卡流量 #sar -n DEV 1 5 [每间隔1秒刷新一次,共5次] sar -n DEV 1 5 IFACE 表示设备名称 rxpck/s 每秒接收的包的数量 txpck/s 每秒 ...

  9. CAT 3.0 开源发布,支持多语言客户端及多项性能提升

    项目背景 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访问 ...

  10. ubuntu 16.04.1 LTS postgresql安装配置

    postgresql安装--------------------二进制安装:wget https://get.enterprisedb.com/postgresql/postgresql-9.5.6- ...