Codeforces Round #292 (Div. 2)--B. Drazil and his Happy Friends

Source: Internet
Author: User
Tags gcd greatest common divisor 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 from0To n?-? 1and0To m?-? 1Separately. InchI-th Day, Drazil invites-th boy and-th girl to has dinner together (as Drazil is programmer,IStarts from0). 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. n and m (1?≤? N,? M.≤?100).

The second line contains integerb(0?≤? b? ≤? N ), denoting the number of happy boys among Friends of Drazil, and then followbDistinct integers x1,? 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 followgDistinct integers y1,? 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 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 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.



Idea: Greatest common divisor, if it is 1, just see if there is a happy, there is yes otherwise no, if greater than 1, then see each length of T (greatest common divisor) (b and G can be divided into a good number of length t interval) each corresponding position at least one is happy



AC Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int gcd (int a, int b) {     if (a > B) return gcd (b, a);      if (b% A = = 0) return A;      Return gcd (b% A, a);    












Codeforces Round #292 (Div. 2)--B. Drazil and his Happy Friends

Contact Us

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.