SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber
题目: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的更多相关文章
- SRM 620 D2L3: RandomGraph, dp
称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...
- poj 3537 Crosses and Crosses 博弈论之grundy值
题意: 给1*n的格子,轮流在上面叉叉,最先画得3个连续叉叉的赢.问先手必胜还是必败. 分析: 求状态的grundy值(也就是sg值),详细怎么求详见代码.为什么这么求要自己想的,仅仅可意会(别人都说 ...
- topcoder SRM 624 DIV2 BuildingHeightsEasy
从大到小遍历一遍,每次取M个元素,然后求得最小的floor即可 int minimum(int M, vector <int> heights) { sort(heights.begin( ...
- topcoder SRM 624 DIV2 CostOfDancing
排个序,求前k个元素和即可 int minimum(int K, vector <int> danceCost) { sort(danceCost.begin(),danceCost.en ...
- SRM 624 Building Heights DivI 解读
几乎相同的一标题.欲了解更多请参阅:http://community.topcoder.com/stat?c=problem_statement&pm=13211&rd=15857 思 ...
- SRM 621 D2L3: MixingColors, math
题目:http://community.topcoder.com/stat? c=problem_statement&pm=10409&rd=15854 利用高斯消元求线性空间的基,也 ...
- SRM 622 D2L3: Subsets, math, backtrack
题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855 符合条件的集中非1的元素个数是非 ...
- TC250专场
SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...
- 博弈论:寻找先手必胜策略——Grundy值
选修了人工智能课程,老师布置了调研任务:Grundy,开始看了一些资料并没有看懂. 后来找到了一篇文,写的很棒,里面有好多博弈相关的问题与分析,分享出来给大家: http://endless.logd ...
随机推荐
- chmod g+s 、chmod o+t 、chmod u+s:Linux高级权限管理
关于linux下权限操作chmod的一些说明!比rxw高级内容! 转载自http://blog.chinaunix.net/uid-26642180-id-3378119.html Set uid, ...
- 两类for循环
九.两类for循环 (一)穷举 1.格式 for (初始条件;循环条件 ;循环改变) { for (初始条件;循环条件;循环改变) { for (初始条件;循环条件;循环改变) { if (判断条件) ...
- Monaco Editor 使用入门
以前项目是用ace编辑器的,但是总有些不敬人意的地方.前端事件看见的VS Code编辑器Monaco Editor准备更换下,下面介绍一些使用中遇到的一点问题.代码提示 1.项目引用 import * ...
- python开发学习-day16(Django框架初识)
s12-20160507-day16 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- python开发学习-day13(js、jQuery)
s12-20160409-day13 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- 【LOJ】#2320. 「清华集训 2017」生成树计数
题解 我,理解题解,用了一天 我,卡常数,又用了一天 到了最后,我才发现,我有个加法取模,写的是while(c >= MOD) c -= MOD 我把while改成if,时间,少了 六倍. 六倍 ...
- Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting
G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE..... 需要每次清空一下子树的map... #inc ...
- 如何让 C++ 和 C# 一样易用,而且效率更高?
(未完成,待续..) 第一章:C++基本功 1. 养成良好的编码习惯. 2. 堆上的资源随用随还,严格控制生命周期. 第二章:一些调试心得 1. 在VisualStudio环境下,曾出现 Detect ...
- 装饰 Markdown
利用 Font Awesome 提升 Markdown 的表现能力 Font Awesome 是一个字体和图标工具包,包含人物.动物.建筑.商业.品牌等等各种主题丰富的图标符号,可以通过相应的语法添加 ...
- imageio 载入 Buffer 格式的图片
题注:OpenCV 使用 pip install -U opencv-python 即可安装. import zipfile import imageio import cv2 # 载入压缩文件 Z ...