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 3
0
1 0
Output
Yes
Input
2 4
1 0
1 2
Output
No
Input
2 3
1 0
1 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 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。