Description

The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,..., n - 1. Two persons pick stones in turn. In every turn, each person selects three piles of stones numbered ijk (i < jjk and at least one stone left in pile i). Then, the person gets one stone out of pile i, and put one stone into pile j and pile k respectively. (Note: if j = k, it will be the same as putting two stones into pile j). One will fail if he can't pick stones according to the rule.

David is the player who first picks stones and he hopes to win the game. Can you write a program to help him?

The number of piles, n, does not exceed 23. The number of stones in each pile does not exceed 1000. Suppose the opponent player is very smart and he will follow the optimized strategy to pick stones.

Input

Input contains several cases. Each case has two lines. The first line contains a positive integer n ( 1 n 23) indicating the number of piles of stones. The second line contains n non-negative integers separated by blanks, S0,...Sn-1 ( 0 Si 1000), indicating the number of stones in pile 0 to pile n - 1respectively.

The last case is followed by a line containing a zero.

Output

For each case, output a line in the format `` Game tijk". t is the case number. ij and k indicates which three piles David shall select at the first step if he wants to win. If there are multiple groups of ij and k, output the group with the minimized lexicographic order. If there are no strategies to win the game, ijand k are equal to -1.
 
题目大意:有n堆石头,第i堆石头有Si个石头,每次可以从第i堆石头拿掉一个石头,然后在第j、k(i<j、k)上各放一个石头。两人轮流操作,不能操作的人算输,问先手第一步的必胜策略是什么,输出最小字典序的答案。
思路:利用SG函数来做。可以把每个石头看作是独立的游戏,那么对于某个在第i位的石头,它的后续状态是所有的在第j、k位的两个石头,这两个石头也是独立的游戏,那么sg[i] = mex{sg[j] ^ sg[k]},对于每个位置的sg[i]可以O(n^3)预先计算出来。
然后所有的石头的SG值异或若等于0则输出-1-1-1(对于每一个Si,若Si为奇数,则异或一次,若Si为偶数,则不异或,因为一个数异或偶数次,肯定是0,复杂度为O(n)),否则O(n^3)暴力枚举方案,若sg[i]^sg[j]^sg[k]^ans==0,则i、j、k是必胜方案,其中ans是所有石头SG值异或的结果。
就是一步操作i、j、k,sg[i]会变成sg[j]^sg[k],那么若原来的SG值为ans,又sg[i]^sg[j]^sg[k]^ans==0,那么新状态的SG值就为0(可以参考NIM博弈的证明)。
 
代码(3MS):
 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; int sg[MAXN + ];
bool mex[MAXN * MAXN];
int n, s[MAXN]; void build_sg() {
for(int i = MAXN - ; i >= ; --i) {
memset(mex, , sizeof(mex));
for(int j = i + ; j < MAXN; ++j) {
for(int k = j; k < MAXN; ++k) mex[sg[j] ^ sg[k]] = true;
}
for(int j = ; ; ++j)
if(!mex[j]) {sg[i] = j; break;}
}
} void solve() {
int ans = , *sg = ::sg + MAXN - n;
for(int i = ; i < n; ++i) ans ^= sg[i] * (s[i] & );
if(ans == ) {
printf(" -1 -1 -1\n");
return ;
}
for(int i = ; i < n; ++i) {
if(s[i] == ) continue;
for(int j = i + ; j < n; ++j) {
for(int k = j; k < n; ++k) {
if((sg[i] ^ sg[j] ^ sg[k] ^ ans) == ) {
printf(" %d %d %d\n", i, j, k);
return ;
}
}
}
}
} int main() {
build_sg();
int cnt = ;
while(scanf("%d", &n) != EOF && n) {
for(int i = ; i < n; ++i) scanf("%d", &s[i]);
printf("Game %d:", ++cnt);
solve();
}
}

最新文章

  1. echarts一个页面动态加载两张不同图表数据
  2. C# 计时器
  3. MYSQL将表名称修改成大写的存储过程
  4. STL(pair map set vector priority_queue) poj 3297
  5. WordPress Contact Form 7插件任意文件上传漏洞
  6. 关于pagerank算法的一点点总结
  7. 解决XCode插件在XCode6.4上失效的办法
  8. 支付宝即时到账DEMO配置与使用
  9. sql server 高可用镜像
  10. 搭建docker私有仓库(https)
  11. faiss索引基于数量级和内存限制的选择
  12. 解码base64加密的图片并打印到前台
  13. 解决kali linux 升级后安装w3af 问题
  14. json-server基本使用
  15. 深入JVM之类的加载过程
  16. Web API 2 对于 Content-Length 要求严格
  17. parallelogram
  18. 【C++】atoi与stoi
  19. Java applets A Java applet example
  20. ES6 之 解构赋值

热门文章

  1. github常见操作和常见错误
  2. js中哪些值会被认为false?
  3. 集群、RAC和MAA
  4. javascript最常用的对象创建方式
  5. flask第三方插件WTForms
  6. 使用Screen管理远程会话
  7. PHP封装CURL
  8. hadoop生态搭建(3节点)-12.rabbitmq配置
  9. python学习——函数进阶
  10. avast:中兴手机预装恶意软件 嵌入固件底层