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 k2elements. One of them goes first and the player changes alternatively. In each player's turn, he should choose an arbitrary number like xfrom 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.
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.
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.
5 2 3 2 3 1 2 3
Lose Win Win Loop Loop Win Win Win
8 4 6 2 3 4 2 3 6
Win Win Win Win Win Win Win Lose Win Lose Lose Win Lose Lose
题意:1~n成一个环,现在从2~n中的某个点出发,两个人玩游戏,每个人可走的步数分别是大小为k1和k2的集合,两个人轮流顺时针走,谁先走到1谁赢。求任意一个人先手,起点在任意点的先手输赢情况(可能为平局)。
思路:逆序bfs,从结果出发bfs遍历,并存储在结果数组中,如果前一种状态是lose,那么逆序的这种状态是win,入队;如果前一种状态是win,那么这个状态的出度减一,直到出度为0时,状态为lose,入队。没有遍历到的点是loop。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n;
int k[2];
int s[2][7005];
int ans[2][7005];
int deg[2][7005];
struct node
{
int pla,p;
bool flag;
};
void bfs(int pla)
{
queue<node>que;
while(!que.empty())que.pop();
node u,v;
u.flag=false;
u.p=1;
u.pla=pla;
que.push(u);
int pl2;
while(!que.empty())
{
v=que.front();
que.pop();
ans[v.pla][v.p]=v.flag;
pl2=v.pla^1;
if(v.flag==false)
{
for(int i=0;i<k[pl2];i++)
{
u.pla=pl2;
u.p=(v.p-s[pl2][i]+n)%n;
if(u.p==0)u.p=n;
u.flag=true;
if(u.p!=1&&ans[u.pla][u.p]!=1)
{
que.push(u);
}
}
}
else
{
for(int i=0;i<k[pl2];i++)
{
u.pla=pl2;
u.p=(v.p-s[pl2][i]+n)%n;
if(u.p==0)u.p=n;
u.flag=false;
deg[pl2][u.p]--;
if(deg[pl2][u.p]==0&&ans[pl2][u.p]!=1)
{
que.push(u);
}
}
}
}
}
void solve(int x)
{
if(x==0)printf("Lose ");
else if(x==1)printf("Win ");
else printf("Loop ");
}
int main()
{
memset(ans,-1,sizeof(ans));
scanf("%d",&n);
scanf("%d",&k[0]);
for(int i=0;i<k[0];i++)scanf("%d",&s[0][i]);
scanf("%d",&k[1]);
for(int i=0;i<k[1];i++)scanf("%d",&s[1][i]);
for(int i=2;i<=n;i++)
{
deg[0][i]=k[0];
deg[1][i]=k[1];
}
bfs(0);
bfs(1);
for(int i=2;i<=n;i++)
{
solve(ans[0][i]);
}
cout<<endl;
for(int i=2;i<=n;i++)
{
solve(ans[1][i]);
}
cout<<endl;
}