http://codeforces.com/problemset/problem/786/A

Description

Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer.

In this game there are n objects numbered from 1 to n arranged in a circle (in clockwise order). Object number 1 is a black hole and the others are planets. There’s a monster in one of the planet. Rick and Morty don’t know on which one yet, only that he’s not initially in the black hole, but Unity will inform them before the game starts. But for now, they want to be prepared for every possible scenario.

Each one of them has a set of numbers between 1 and n - 1 (inclusive). Rick’s set is s1 with k1 elements and Morty’s is s2 with k2 elements. One of them goes first and the player changes alternatively. In each player’s turn, he should choose an arbitrary number like x from his set and the monster will move to his x-th next object from its current position (clockwise). If after his move the monster gets to the black hole he wins.

Your task is that for each of monster’s initial positions and who plays first determine if the starter wins, loses, or the game will stuck in an infinite loop. In case when player can lose or make game infinity, it more profitable to choose infinity game.

Input

The first line of input contains a single integer n (2 ≤ n ≤ 7000) — number of objects in game.

The second line contains integer k1 followed by k1 distinct integers s1, 1, s1, 2, …, s1, k1 — Rick’s set.

The third line contains integer k2 followed by k2 distinct integers s2, 1, s2, 2, …, s2, k2 — Morty’s set

1 ≤ ki ≤ n - 1 and 1 ≤ si, 1, si, 2, …, si, ki ≤ n - 1 for 1 ≤ i ≤ 2.

Output

In the first line print n - 1 words separated by spaces where i-th word is “Win” (without quotations) if in the scenario that Rick plays first and monster is initially in object number i + 1 he wins, “Lose” if he loses and “Loop” if the game will never end.

Similarly, in the second line print n - 1 words separated by spaces where i-th word is “Win” (without quotations) if in the scenario that Morty plays first and monster is initially in object number i + 1 he wins, “Lose” if he loses and “Loop” if the game will never end.

Examples

input
5
2 3 2
3 1 2 3
output
Lose Win Win Loop
Loop Win Win Win
input
8
4 6 2 3 4
2 3 6
output
Win Win Win Win Win Win Win
Lose Win Lose Lose Win Lose Lose

Key

题意:一个有n个结点的环,第一个结点上是黑洞,其他为行星。现在有一个怪物可能在任意的行星上。甲乙两人可以顺时针驱赶怪物。甲每次可驱赶s11,s12,s13...个格子,共k1个可能。乙每次可驱赶s21,s22,s23...个格子,共k2种可能。谁把怪物刚好驱逐到黑洞里就赢。现在求:当怪物在第i个行星,甲(或乙)第一个驱赶时,甲(或乙)一定赢、一定输还是平局?

思路:DFS+动态规划。(这里只考虑甲开局的情况,乙一样求)从黑洞开始反向逆推,即分别倒退s11,s12,s13...个格子,只要从这些点驱逐,甲是必赢的;换句话说,乙如果把怪物驱逐到了这些点,乙就输了,所以当乙能驱赶的点都是必输点时,当前点也是乙的必输点。

那么对于当前点:若能够将怪物驱逐到对手的必输点,该点必赢,对当前点DFS;若能够将怪物驱逐到对手的必赢点,为当前点计数,当当前点到所有可驱逐的点都会导致失败时,当前点即为必输点。

使用sch数组保存点是否访问过,即DP的记忆化搜索的思想。

Code

#include<cstdio>
#include<cstring>
#define N 7008
int n, k[2], s[2][N];
int win[2][N], sch[2][N], cnt[2][N]; void init(int who) {
memset(win[who], 0, sizeof(int)*n);
memset(sch[who], 0, sizeof(int)*n);
memset(cnt[who], 0, sizeof(int)*n);
} void dp(int now, int who) {
sch[!who][now] = 1; // 能够允许dp递归下来的now都是已经确定输和赢的
/*
for (int i = 0;i != n;++i) {
printf("win{%d %d} sch{%d %d} cnt{%d %d}\n",
win[0][i], win[1][i],
sch[0][i], sch[1][i],
cnt[0][i], cnt[1][i]);
}
puts("-----");
*/
for (int i = 0;i != k[who];++i) {
int pre = (now - s[who][i] + n) % n; // who让怪物从pre跳到now
if (!pre) continue; // 不可能
if (sch[who][pre]) continue; // 记忆搜索
if (!win[!who][now]) { // 若从pre到now对面必输. (输的条件是sch[who][i]==1且win[who][i]==0)
win[who][pre] = 1; // 这一步一定赢
dp(pre, !who);
}
else if (win[!who][now]) { // 若从pre到now对面必赢
if (++cnt[who][pre] == k[who]) { // 当这一步怎么走都会导致对面赢
dp(pre, !who);
}
}
}
} int main()
{
scanf("%d", &n);
for (int who = 0;who != 2;++who) {
scanf("%d", k + who);
for (int i = 0;i != k[who];++i)
scanf("%d", s[who] + i);
}
init(0), init(1);
dp(0, 0),dp(0, 1);
for (int who = 0;who != 2;++who) {
for (int i = 1;i != n;++i) {
if (win[who][i]) printf("Win");
else if (sch[who][i]) printf("Lose");
else printf("Loop");
printf("%c", " \n"[i == n - 1]);
}
}
return 0;
}

[刷题]Codeforces 786A - Berzerk的更多相关文章

  1. [刷题]Codeforces 794C - Naming Company

    http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...

  2. [刷题codeforces]650A.637A

    650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...

  3. [刷题codeforces]651B/651A

    651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势 ...

  4. [刷题]Codeforces 746G - New Roads

    Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...

  5. CF刷题-Codeforces Round #481-G. Petya's Exams

    题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...

  6. CF刷题-Codeforces Round #481-F. Mentors

    题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...

  7. CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression

    题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...

  8. Codeforces 786A Berzerk(博弈论)

    [题目链接] http://codeforces.com/problemset/problem/786/A [题目大意] 有两个人,每个人有一个数集,里面有一些数,现在有一个环,有个棋子放在1, 有个 ...

  9. [刷题]Codeforces 785D - Anton and School - 2

    Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...

随机推荐

  1. Dapper源码学习和源码修改(下篇)

    目录: Dapper源码学习和源码修改(上篇主要讲解入参解析) Dapper源码学习和源码修改(下篇主要讲解出参解析) 继上篇讲了下自己学习Dapper的心得之后,下篇也随之而来,上篇主要讲的入参解析 ...

  2. Mac端SVN工具CornerStone详解

    俗话说:"工欲善其事必先利其器": 对于我们程序员来说,不管你是大神,还是小鱼小虾,进入公司之后,都用过源码管理工具,不然你就不是一个合格的程序员,现在各个公司用于源码管理工具通常 ...

  3. java日期转换

    在java开发过程中,时间的转换时必须掌握的=========下面把时间转换做个总结,有可能不是很全面 时间格式只有两种 yyyy-MM-DD yyyy/MM/DD 时间的类型:字符串类型.sql类型 ...

  4. 为什么要学Python

    人生苦短,我用python.在大学四年的本科学习中,Python是我接触过语法最简单,功能最为强大的语言,拥有众多第三方库的支持的语言.如果要选一门编程语言作为入门,建议使用Python.但是为了更加 ...

  5. 机器学习:Python中如何使用支持向量机(SVM)算法

    (简单介绍一下支持向量机,详细介绍尤其是算法过程可以查阅其他资) 在机器学习领域,支持向量机SVM(Support Vector Machine)是一个有监督的学习模型,通常用来进行模式识别.分类(异 ...

  6. Tomcat源码分析(一)

    这段时间简单的看了一下Tomcat的源码,在这里做个笔记!   1. tomcat 架构图 Catalina: tomcat的顶级容器,main()方法中就是通过,创建Catalina 对象实例,来启 ...

  7. CentOS 7 Root用户密码重置 2017-04-02

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 开机的时候按e (如果正在使用,你可以输入reboot,然后赶紧按回车键,也可 ...

  8. Knockout Mvc Compoment FrameSet

    Knockout Mvc Compoment FrameSet 框架文件结构 01-   网站(表现层),mvc主要作用视图展示. 02-   模型(Model),主要作用承载视图数据结构,网站前后台 ...

  9. Reflux中文教程——概览

    翻译自github上的reflux项目,链接:https://github.com/reflux/refluxjs 〇.安装及引入 安装: npm install reflux 引入: var Ref ...

  10. 做一个完整的纯react-naitve安卓应用【从环境安装到应用发布】

    前提:从来没有写过android 跟 ios 应用,是一个小前端.前一段时间玩了一下 react-native 感觉还不错,应用代码还未开源. 环境: win7 成果:                 ...