uva1378 - A Funny Stone Game SG函数

The funny stone game is coming. There are n piles of stones,numbered with0, 1, 2,..., n - 1. Two persons pick stones in turn.In every turn, each person selects three piles of stones numberedi,j, k (i <j, j $ \leq$k and at least one stone left in pilei). Then, the person gets one stone out of pilei, and put onestone into pile j and pile k respectively. (Note: ifj = k, itwill be the same as putting two stones into pilej). One will failif he can't pick stones according to the rule.

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

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

Input 

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

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

Output 

For each case, output a line in the format `` Game t:i j k". t isthe case number. i, j and k indicates which three piles Davidshall select at the first step if he wants to win. If there aremultiple groups of i, j and k, output the group with theminimized lexicographic order. If there are no strategies to win thegame, i, j and k are equal to -1.

Sample Input 

4
1 0 1 100
3
1 0 5
2
2 1
0

Sample Output 

Game 1: 0 2 3
Game 2: 0 1 1
Game 3: -1 -1 -1

  有N堆石子,每次可以选3堆,i<j<=k,且第i堆至少剩一个石子,把第i堆的石子拿走一个,k,j堆各放一个,谁不能操作谁输。

  假设第i堆上有一个石子,其它堆都没有,往左移的话它的子状态就是sg[j]^sg[k],j<i,k<i。

  这个游戏就相当于很多这样的单个石子游戏的NIM和了,如果某一堆偶数个的话异或为0可以忽略,奇数个异或起来也就相当于一个。

  要输出的话,求完SG再暴力一遍,找到满足操作完之后的SG为0的最小字典序操作。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 30
#define MAXM 110
#define MAXNODE 100010*27
#define MOD 999983
typedef long long LL;
using namespace std;
int N,g[MAXN],a[MAXN];
void init(){
    int vis[100];
    g[0]=0;
    for(int i=0;i<=23;i++){
        memset(vis,0,sizeof(vis));
        for(int j=0;j<i;j++)
            for(int k=0;k<=j;k++) vis[g[j]^g[k]]=1;
        for(int j=0;;j++) if(!vis[j]){
            g[i]=j;
            break;
        }
    }
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    init();
    while(scanf("%d",&N)!=EOF&&N){
        printf("Game %d: ",++cas);
        int sg=0;
        for(int i=0;i<N;i++){
            scanf("%d",&a[i]);
            if(a[i]%2) sg^=g[N-i-1];
        }
        if(!sg){
            printf("-1 -1 -1\n");
            continue;
        }
        int find=0;
        for(int i=0;i<N;i++) if(a[i]){
            for(int j=i+1;j<N;j++){
                for(int k=j;k<N;k++){
                    if(sg==(g[N-i-1]^g[N-j-1]^g[N-k-1])){
                        printf("%d %d %d\n",i,j,k);
                        find=1;
                        break;
                    }
                }
                if(find) break;
            }
            if(find) break;
        }
    }
    return 0;
}



 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值