码迷,mamicode.com
首页 > 移动开发 > 详细

B. Drazil and His Happy Friends

时间:2015-02-18 18:44:57      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

B. Drazil and His Happy Friends

标签:

原文地址:http://www.cnblogs.com/wsaaaaa/p/4295861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!