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 题意: 给定一个数,将其拆分成两个数的乘 ...
随机推荐
- Windows对python文件加密
最近项目需要对部分python文件加密,调研了部分方法都觉得不可行,最后采用了将python转换成so文件.pyd文件的方法.so文件,为liunx下的动态链接库文件,在windows下为dll文件, ...
- java多线程回顾1:线程的概念与创建
1.进程与线程的概念 现在几乎所有操作系统都支持多任务,通常一个任务就是一个程序,一个运行中的程序就是一个进程.当一个程序行时,其内部也可能在执行多个任务,进程内每一个任务的执行流,就是一个线程. 所 ...
- Spring Boot2 系列教程(二十三)理解 Spring Data Jpa
有很多读者留言希望松哥能好好聊聊 Spring Data Jpa! 其实这个话题松哥以前零零散散的介绍过,在我的书里也有介绍过,但是在公众号中还没和大伙聊过,因此本文就和大家来仔细聊聊 Spring ...
- 大宇java面试系列(一):jvm垃圾回收
1. 说一下 JVM 有哪些垃圾回收算法? 标记-清除算法:标记无用对象,然后进行清除回收.缺点:效率不高,无法清除垃圾碎片. 标记-整理算法:标记无用对象,让所有存活的对象都向一端移动,然后直接清除 ...
- Error response from daemon ... no space left on device docker启动容器服务报错
docker 启动容器服务的时候,报错no space left on device 1. 检查磁盘是否用光 3.检查inode是否耗光,从截图看到是inode耗光导致出现问题: 进入到/run里面看 ...
- mysql 创建用户及授权(1)
一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...
- API网关在API安全性中的作用
从单一应用程序切换到微服务时,客户端的行为不能与客户端具有该应用程序的一个入口点的行为相同.简单来说就是微服务上的某一部分功能与单独实现该应用程序时存在不同. 目前在使用微服务时,客户端必须处理微服务 ...
- golang学习--go中'继承'和多态
golang中没有继承的概念,这个struct属性上的继承,但是可以用匿名字段来模拟这个过程,方法上面的继承要使用接口.多态可以通过接口实现.可以看一下代码. package main import ...
- caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads
本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...
- LaravelS - 基于Swoole加速Laravel/Lumen
LaravelS LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能.更多可能性.Github 特性 内置Http/WebSocket服务器 ...