UVALive 3668 A Funny Stone Game

题目链接:UVALive - 3668

题目大意为给定n堆石子,每次的操作是选择三个数i<j<=k,从i中拿一枚石子,在j和k中分别放入一枚石子。不能操作者输。求先手是否能赢,若可以,则输出字典序最小的第一步操作。

思路是把在每个位置上的每颗石子当成一个游戏。

用SG[i]表示在第i堆中的一颗石子的sg函数。

则SG[i]=mex(SG[j] ^ SG[k])。

然后异或求游戏的和即可。

为找到字典序最小的第一步操作,我们枚举第一步操作,然后求游戏的和即可。

代码如下:

 1 #include"cstdio"
 2 #include"iostream"
 3 #include"cstring"
 4 #include"algorithm"
 5 #include"cstdlib"
 6 #include"vector"
 7 #include"set"
 8 #include"map"
 9 #include"cmath"
10 using namespace std;
11 typedef long long LL;
12 const LL MAXN=30;
13
14 int n;
15 int f[MAXN];
16 int sg(int x)
17 {
18     if(x==n) return 0;
19     if(f[x]!=-1) return f[x];
20     int vis[200];
21     memset(vis,0,sizeof(vis));
22     for(int i=x+1;i<=n;i++)
23         for(int j=i;j<=n;j++)
24             vis[sg(i)^sg(j)]=1;
25     for(int i=0;i<200;i++)
26         if(!vis[i])
27             return f[x]=i;
28     return 200;
29 }
30 int d[MAXN];
31 int cal()
32 {
33     memset(f,-1,sizeof(f));
34     int ans=0;
35     for(int i=1;i<=n;i++)
36         if(d[i]%2!=0)
37             ans ^= sg(i);
38     return ans;
39 }
40 int main()
41 {
42 #ifdef LOCAL
43     freopen("in.txt","r",stdin);
44     // freopen("out.txt","w",stdout);
45 #endif
46     int t=0;
47     while(scanf("%d",&n)!=EOF && n)
48     {
49         for(int i=1;i<=n;i++)
50             scanf("%d",&d[i]);
51         int i,j,k;
52         bool ok=false;
53         for(i=1;!ok && i<=n;i++)
54             for(j=i+1;!ok && j<=n;j++)
55                 for(k=j;!ok && k<=n;k++)
56                     if(d[i]>0)
57                     {
58                         d[i]--;
59                         d[j]++;
60                         d[k]++;
61                         if(cal()==0)
62                             ok=true;
63                         d[i]++;
64                         d[j]--;
65                         d[k]--;
66                     }
67         printf("Game %d: ",++t);
68         if(ok) printf("%d %d %d\n",i-2,j-2,k-2);
69         else printf("-1 -1 -1\n");
70     }
71     return 0;
72 }
时间: 2024-08-27 23:16:55

UVALive 3668 A Funny Stone Game的相关文章

[nim博弈扩展 sg函数] UVALive 3668 A Funny Stone Game

题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=242&page=show_problem&problem=1669 Root :: Regionals 2006 :: Asia - Beijing    Regionals 2006 >> Asia - Beijing 3668 - A Funny Stone Game Tim

UVALive 5059 C - Playing With Stones 博弈论Sg函数

C - Playing With Stones Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5059 Description You and your friend are playing a game in which you and your friend take turns removing stones from piles.

POJ 1740 A New Stone Game

A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 Description Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob

Lifting the Stone(hdoj1115)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6104    Accepted Submission(s): 2546 Problem Description There are many secret openings in the floor which are covered by a big

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

Light OJ 1296 - Again Stone Game

传送门 1296 - Again Stone Game    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they a