题目: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

最新文章

  1. Asterisk manager API(AMI)文档(中文版)
  2. SSRS报表参数设置
  3. jquery 获取元素坐标
  4. Mysql InnoDB彻底释放磁盘空间
  5. Oulipo
  6. oracle实现远程连接超简单;枚举与剪枝();PowerDesigner生成数据库代码注意里面的双引號,应该去掉
  7. webpack-dev-server 搭建本地服务以及浏览器实时刷新
  8. 关于使用Unity开发Kinect时出现的Runtime Error错误的解决方式
  9. mysql用limit时offset越大时间越长
  10. azkaban报错记录
  11. JSPatch 热更新
  12. Spring Boot 系列(七)Swagger2-生成RESTful接口文档
  13. ARC062 - F. Painting Graphs with AtCoDeer (Polya+点双联通分量)
  14. Linux学习之常用权限管理命令(二)
  15. C和C指针小记(十)-函数
  16. jsoi2018 R1R2
  17. VSFTP再配置 我里个去马蛋网上这么多烂文章,走了好多弯路
  18. pandas 中的DataFrame.where()使用
  19. Bone Collector--hdu2602(01背包)
  20. Vue2.0的动画效果

热门文章

  1. CentOS7 64位下MySQL5.7安装与配置(YUM)转
  2. 两种方法设置nginx并发限制下面的白名单策略
  3. 【LOJ】#2037. 「SHOI2015」脑洞治疗仪
  4. 【POJ】2454.Jersey Politics
  5. USACO 5.5 Hidden Password
  6. java内存溢出分析工具
  7. nginx、php-fpm、swoole HTTP/TCP压测对比
  8. CodeForces 140C New Year Snowmen(堆)
  9. HDU 6103 Kirinriki (思维 双指针)
  10. Highmaps网页图表教程之图表配置项结构与商业授权