B. Drazil and His Happy Friends

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.

There are n boys and m girls among his friends. Let‘s number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.

Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.

Input

The first line contains two integer n and m (1 ≤ n, m ≤ 100).

The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.

The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.

It is guaranteed that there is at least one person that is unhappy among his friends.

Output

If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".

Sample test(s)

Input

2 301 0

Output

Yes

Input

2 41 01 2

Output

No

Input

2 31 01 1

Output

Yes

Note

By we define the remainder of integer division of i by k.

In first sample case:

  • On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
  • On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
  • On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
  • On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
  • On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.

题意:共有n个男孩和m个女孩,从0~n-1he0~m-1编号,在给出二者中哪些人是开心的,按照i%n和i%m取编号凡其中一个开心则另一个也开心,判断能否让所有人都开心。

分析:暴力过,根据数据即使每回计算最大次数10000次也不会超过时间。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4
 5 using namespace std;
 6
 7 int main()
 8 {
 9    int a[200],b[200];
10    int n,m;
11    cin>>n>>m;
12    memset(a,0,sizeof(a));
13    memset(b,0,sizeof(b));
14
15    int num;
16    cin>>num;
17    for(int i=0;i<num;i++) {int p;cin>>p;a[p]=1;}
18    cin>>num;
19    for(int i=0;i<num;i++) {int p;cin>>p;b[p]=1;}
20
21    for(int i=0;i<10000;i++)
22        if(a[i%n]||b[i%m]) a[i%n]=b[i%m]=1;
23    int flag=1;
24    for(int i=0;i<n||i<m;i++)
25    {
26        if(i<n&&!a[i%n]) {flag=0;break;}
27        if(i<m&&!b[i%m]) {flag=0;break;}
28    }
29    if(flag) cout<<"Yes"<<endl;
30    else cout<<"No"<<endl;
31 }
时间: 2024-10-16 07:38:32

B. Drazil and His Happy Friends的相关文章

#292 (div.2) D.Drazil and Tiles (贪心+bfs)

Description Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should

Codeforces 515C. Drazil and Factorial

//codeforces 515C. Drazil and Factorial #include <iostream> #include <cstring> #include <cstdio> using namespace std; /** 4!=2!*2!*3! 6!=1*2*3*4*5*6=5!*3! 8!=1*2*3*4*5*6*7*8=7!*2!*2!*2! 9!=1*2*3*4*5*6*7*8*9=7!*2!*3!*3! */ int main() { ch

CF Drazil and His Happy Friends

Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his frie

codeforces 515B.Drazil and His Happy Friends

B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his f

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

CodeForces515B Drazil and His Happy Friends (数学)

B. Drazil and His Happy Friends time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his frien

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil created a following problem about putting 1?×?2 tiles into an n?×?m grid: "There is a grid with some cells that a

Codeforces Round #292 (Div. 1)---A. Drazil and Factorial

Drazil is playing a math game with Varda. Let's define for positive integer x as a product of factorials of its digits. For example, . First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This n

Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

B. Drazil and Tiles Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tile