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 题意: 给定一个数,将其拆分成两个数的乘 ...
随机推荐
- Python 面向对象之一 类与属性
Python 面向对象之 类与属性 今天接触了一下面向对象,发现面向对象和之前理解的简直就是天壤之别,在学Linux的时候,一切皆文件,现在学面向对象了,so,一切皆对象. 之前不是一直在学的用面向函 ...
- CentOS7 编码编译安装或卸载http2.4.25 一键脚本
待完善 CentOS 7测试 哈哈 #!/bin/bash #************************************************************** #Autho ...
- 网络编程--UDP通讯
UTP传输 public class Send1 { public static void main(String[] args) throws Exception { Scanner sc=new ...
- PHP 教你使用 Swoole-Tracker 秒级定位 PHP 卡死问题
PHPer 肯定收到过这样的投诉:小菊花一直在转!你们网站怎么这么卡!当我们线上业务遇到这种卡住(阻塞)的情况,大部分 PHPer 会两眼一抹黑,随后想起那句名言:性能瓶颈都在数据库然后把锅甩给DBA ...
- hdu 1068 Girls and Boys (最大独立集)
Girls and BoysTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- gdb(ddd,kdevelop等)调试ZeroIce开发的应用程序,中断信号引起的问题
不作文,只记要点. 1.Ice::Application的程序框架默认对SIGHUP, SIGINT, SIGTERM进行处理.目的就是捕捉Ctrl+C发出信号有序地结束程序.这个功能扰乱了我们使用g ...
- elementui分页记录,reserve-selection
第一步:在<el-table></el-table>标签中加上 :row-key="getRowKeys" 第二步:在<el-table-column ...
- 【Luogu P1631】序列合并
Luogu P1631 题意很好懂,不作分析 很容易想出一个解法是求出每一个和,排序后取前n个. 当然这种做法妥妥的会MLE+TLE 我们会发现实质上这种做法的缺点在于存入了大量不需要的数据. 那么该 ...
- urllib练习
# coding = utf-8 """ 解析https://www.kuaidaili.com/free/网站里的代理地址, 并测试是否可用 ""& ...
- Spring IOC初始化深度解析
1.前言 本文是基于JAVA配置方法对Spring IOC进行分析,掌握Spring IOC初始化流程对于我们更好的使用Spring.学习Spring还是很有帮助的,本文所使用的的Spring版本为5 ...