# B. Drazil and his Happy Friends

Source: Internet
Author: User
Tags integer division

B. Drazil and his Happy friendstime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputstan Dard output

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

There isNBoys andmGirls among his friends. Let's number them from0ToN-1and0Tom-1 separately. In  i -th day, Drazil invites -th boy And -th girl to has dinner together (as Drazil is Programmer,  i  starts from 0). If One of those people is happy, the other one would also become happy. Otherwise, those, people remain in their states. Once a person becomes happy (or if he/she is happy originally), he stays happy forever.

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

Input

The first line contains both integer n and M (1≤ n, m ≤ ).

The second line contains integerb(0≤ b n ), denoting the number of happy boys among Friends of Drazil, and then Follow&nbs P b  distinct integers  x 1, x 2, ..., x b   (0≤ x i < n ), denoting the list of indices of Happy boys.

The third line Conatins integerg(0≤ g m ), denoting the number of happy girls among Friends of Drazil, and then Follow&nb Sp G  distinct integers  y 1, y 2, ..., y g   (0≤ y J < m ), denoting the list of indices of happy girls.

It is guaranteed, that there are at least one person, which is unhappy among he friends.

Output

If Drazil can make all the friends become happy by the 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 an 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 in 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 is both unhappy, so nothing changes on this day.
• On the 2-nd day, Drazil invites 0-th Boy and 2-nd girl. Because 0-th Boy was 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 was happy, so he makes the 1-st girl happy. So, all friends become happy at the this moment.

The main problem:

Drazil has a lot of friends and he wants to make all his friends happy.

So he thought of a way to the number of his male and female friends, according to the date divided by the number of the order of the remainder to invite friends, invite friends to his home to play.

If one person is happy each time they come, the other person will become happy, and the happiness is permanent, enter the data and judge whether his goal can be achieved.

Code

```/*author:ovreetime:2015 February 25 20:46:57*/#include<iostream>using namespacestd;intMain () {intm, N, B, G; intFlag =1; CIN>> m >>N; /*int Pub;//ask M, n least common multiple for (int i = 1;;            i++) {if (i%m ==0 && i%n ==0) {pub = i;        Break    }} It is not necessary to find out that the least common multiple traversal is still unable to guarantee the final result, because the happy pass is not only once, but only if the least common multiple of M and N is not themselves, can use pub as the boundary of the cycle; for the sake of the bottom loop I use 10000 as the boundary directly, Even 99 and 100 are guaranteed to repeat as many times as necessary.*/    intboy[ the] = {0}; intgirl[ the]  = {0}; intInd//used to save the subscript;CIN >>b;  for(inti =0; I < b; i++) {cin>>IND; Boy[ind]=1; } CIN>>G;  for(intj =0; J < G; J + +) {cin>>IND; Girl[ind]=1; }     for(inti =0; I <10000; i++)//use 10000 as the loop boundary    {        if(Boy[i%m] | | girl[i%N]) {boy[i%M] =1; Girl[i%n] =1; }    }    //iterate through the array boy and girl; If 0 is present, there are people unhappy and the plan is unsuccessful .     for(inti =0; I < m;i++)    {        if(Boy[i] = =0) {flag=0;  Break; }    }     for(inti =0; I < n;i++)    {        if(Girl[i] = =0) {flag=0;  Break; }    }    if(flag) cout<<"YES"; Elsecout<<"NO"; return 0;}/*     ^          ^    / \        / )   /  |  / /       ^   |           Z ____<//> |     |  //Y \//|  > < | </(o ^ o) | < > ________ N///^//\ \ \_/(_/|//\/_//<___/--<____/___/
*/```

B. Drazil and his Happy Friends

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.