LightOJ 1344 Aladdin and the Game of Bracelets
It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the fourth mystery.
In the cave, Aladdin was moving forward after passing the pathway of magical stones. He found a door and he was about to open the door, but suddenly a Genie appeared and he addressed himself as the guardian of the door. He challenged Aladdin to play a game and promised that he would let Aladdin go through the door, if Aladdin can defeat the Genie. Aladdin defeated the Genie and continued his journey. However, let's concentrate on the game.
The game was called 'Game of Bracelets'. A bracelet is a linear chain of some pearls of various weights. The rules of the game are:
1) There are n bracelets.
2) Players alternate turns.
3) In each turn, a player has to choose a pearl from any bracelet. Then all the pearls from that bracelet, that werenot lighter than the pearl, will be removed. It may create some smaller bracelets or the bracelet will be removed if no pearl is left in the bracelet.
4) The player, who cannot take a pearl in his turn, loses.
For example, two bracelets are: 5-1-7-2-4-5-3 and 2-1-5-3, here the integers denote the weights of the pearls. Suppose a player has chosen the first pearl (weight 5) from the first bracelet. Then all the pearls that are not lighter than 5, will be removed (from first bracelet). So, 5-1-7-2-4-5-3, the red ones will be removed and thus from this bracelet, three new bracelets will be formed, 1, 2-4 and 3. So, in the next turn the other player will have four bracelets: 1, 2-4, 3 and 2-1-5-3. Now if a player chooses the only pearl (weight 3) from the third bracelet, then the bracelet will be removed.
Now you are given the information of the bracelets. Assume that Aladdin plays first, and Aladdin and the Genie both play optimally. Your task is to find the winner.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 50). Each of the next n lines contains an integer Ki (1 ≤ Ki ≤ 50) followed by Ki integers, denoting the weights of the pearls of the ith (1 ≤ i ≤ n) bracelet. Each weight will lie in the range [1, 105].
Output
For each case, print the case number and 'Aladdin' if Aladdin wins. Otherwise print 'Genie'. If Aladdin wins, then print an additional line, denoting the weight of pearls, one of which should be chosen in the first move by Aladdin such that he can win. Each pearl should be denoted by a pair of integers: the first integer is the bracelet number and the second integer is the weight. Sort the pearls first by the bracelet number then by the weight. And same weight in a bracelet should be reported once. Check the samples for exact formatting.
Sample Input
4
2
7 5 1 7 2 4 5 3
4 2 1 5 4
2
2 5 2
2 5 2
1
5 5 2 5 2 5
3
5 5 2 5 2 5
5 7 2 7 3 2
4 5 1 5 4
Sample Output
Case 1: Aladdin
(2 5)
Case 2: Genie
Case 3: Aladdin
(1 2)(1 5)
Case 4: Aladdin
(2 7)(3 1)(3 5)
题意:题目意思就是给你n串珍珠,没串珍珠有若干数量,现在两个人进行博弈;轮流进行操作;每次操作选择一个数,然后该珍珠里面大于等于该数的珍珠都会被拿走,此时,该珍珠会被分成其他几个珍珠;
最后谁不能拿,谁输;
题解:每个手镯都是独立的,因此可以异或每个手镯的 sg 值求解。而每个手镯,可以在某些结点被取走珠子,变成新的几段,任意一种情况的 sg 值,便是新分成的几段的 sg 值异或起来。再将每一种情况的 sg 值,记录在 vis 中,查找没出现过的最小的值,便是这个手镯的 sg 值。
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int sg[maxn][maxn];
int arr[maxn][maxn], num[maxn];
int sgtmp[maxn];
int ret[maxn][maxn]; struct node {
int x, y;
} output[maxn * maxn];
bool operator==(node a,node b){return a.x==b.x&&arr[a.x][a.y]==arr[b.x][b.y];}
bool cmp(node a,node b)
{
if(a.x==b.x) return arr[a.x][a.y]<arr[b.x][b.y];
return a.x<b.x;
} int dfs(int now,int l,int r)
{
if(l>r) return ;
if(l==r) return sg[l][r]=;
if(sg[l][r]!=-) return sg[l][r]; int vis[maxn];
memset(vis,,sizeof(vis)); for(int i=l;i<=r;++i)
{
int tmp=,last=-; for(int j=l;j<=r;++j)
{
if(arr[now][j]<arr[now][i])
{
last = j;
break;
}
} for(int j = last + ; j <= r && last != -; ++j)
{
if(arr[now][j] >= arr[now][i])
{
tmp ^= dfs(now, last, j - );
last = -;
for (int k = j + ; k <= r; ++k)
{
if (arr[now][k] < arr[now][i])
{
last = j = k;
break;
}
}
}
}
if(last != -) tmp ^= dfs(now, last, r);
vis[tmp] = ;
if (l == && r == num[now]) ret[now][i] = tmp;
} for(int i = ;;++i)
{
if(vis[i]==)
{
sg[l][r]=i;
return i;
}
}
} int main()
{
int t, k, ca = ;
scanf("%d", &t);
while (t--)
{
int ans = ;
scanf("%d", &k);
for(int i = ; i <= k; ++i)
{
memset(sg, -, sizeof(sg));
scanf("%d", &num[i]);
for(int j = ; j <= num[i]; ++j) scanf("%d", &arr[i][j]); sgtmp[i] = dfs(i, , num[i]);
ans ^= sgtmp[i];
}
if (ans == ) printf("Case %d: Genie\n", ca++);
else {
int cnt = ;
printf("Case %d: Aladdin\n", ca++);
memset(output, , sizeof(output));
for (int i = ; i <= k; ++i)
for (int j=;j<=num[i];++j){if((ans^sgtmp[i]^ret[i][j])==) output[cnt++] = {i, j};}
sort(output,output+cnt,cmp);
cnt=(int)(unique(output,output+cnt)-output);
for(int i = ; i < cnt; ++i)
printf("(%d %d)", output[i].x, arr[output[i].x][output[i].y]);
printf("\n");
}
}
return ;
}
LightOJ 1344 Aladdin and the Game of Bracelets的更多相关文章
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- LightOJ 1348 Aladdin and the Return Journey
Aladdin and the Return Journey Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
- Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)
Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't wa ...
- LightOJ - 1349 - Aladdin and the Optimal Invitation
链接: https://vjudge.net/problem/LightOJ-1349 题意: Finally Aladdin reached home, with the great magical ...
- LightOJ 1341 - Aladdin and the Flying Carpet
题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...
随机推荐
- 护网杯web
首先进入网页后,观察到有sign up 点击sign up 进行注册 再点击sign in 进行登录 进入一个买辣条的界面,可以知道,5元可以买一包大辣条,多包大辣条可以换一包辣条之王,多包辣条之王可 ...
- 【转载】常见十大经典排序算法及C语言实现【附动图图解】
原文链接:https://www.cnblogs.com/onepixel/p/7674659.html 注意: 原文中的算法实现都是基于JS,本文全部修改为C实现,并且统一排序接口,另外增加了一些描 ...
- IDEA快捷键汇总
[常用] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...
- SqlServer2005 查询 第七讲 order by
今天我们来讲sql命令中的参数order by的用法 order by order by:可以理解成[以某个字段排序] order by a,b // a和b都按升序 order by a,b des ...
- [LC]141题 Linked List Cycle (环形链表)(链表)
①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...
- PowerMock学习(四)之Mock static的使用
我们编写代码的时候,总会写一些工具类,为了方便调用喜欢使用static关键字来修饰对应方法. 那么现在举例说明,还是准备两个接口,第一个是查询学生总数,第二个是新增学生两个接口,具体示例代码如下: p ...
- for循环的更多写法
在看设计模式这本书,遇到一个令人疑惑的for循环语句 for (var i = 0, type;type = ['String', 'Array', 'Number'][i++]) { 代码块 } 比 ...
- .NET高级特性-Emit(2)类的定义
在上一篇博文发了一天左右的时间,就收到了博客园许多读者的评论和推荐,非常感谢,我也会及时回复读者的评论.之后我也将继续撰写博文,梳理相关.NET的知识,希望.NET的圈子能越来越大,开发者能了解/深入 ...
- Nginx+SpringBoot实现负载均衡
前言 在上一篇中介绍了Nginx的安装,本篇文章主要介绍的是Nginx如何实现负载均衡. 负载均衡介绍 介绍 在介绍Nginx的负载均衡实现之前,先简单的说下负载均衡的分类,主要分为硬件负载均衡和软件 ...
- 使用 Topshelf 组件一步一步创建 Windows 服务 (2) 使用Quartz.net 调度
上一篇说了如何使用 Topshelf 组件快速创建Windows服务,接下来介绍如何使用 Quartz.net 关于Quartz.net的好处,网上搜索都是一大把一大把的,我就不再多介绍. 先介绍需要 ...