codeforces515b Drazil and his Happy Friends (mathematics)

Source: Internet
Author: User

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 is happy and Some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.

There is 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 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?≤?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 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.

Title Description
There are some boys and girls, some of which are happy. The protagonist from the boys No. 0 and No. 0 start every time to call a home to eat = = if one of them is happy then two people are changed happy otherwise all unhappy home to ask the final protagonist can let everyone happy.

Thinking of solving problems
Because the data is small, violent simulations are possible.
can also find out the number of boys and girls gcd, if gcd=1 so all people have a happy finally can let everyone happy. If the gcd!=1 assumes that gcd=t so that the T-person tied together as a person is also possible, as long as you see the number of individual t, in all groups is not at least one is happy. If the i is not a happy, then certainly this I number to the end also no one and he happy =

The code is as follows

#include <cstdio>Const intMAXN = the;intBOY[MAXN];intGIRL[MAXN];intgcdintAintb) {returnb==0? A:GCD (b,a%b);}intMain () {intB,g;scanf("%d%d", &b,&g);intCntscanf("%d", &cnt);intCHK1 = CNT; while(cnt--) {intAscanf("%d", &a); Boy[a] =1; }scanf("%d", &cnt);intCHK2 = CNT; while(cnt--) {intAscanf("%d", &a); Girl[a] =1; }intt = gcd (B,G);if(T = =1) {if(Chk1 | | chk2)printf("yes\n");Else printf("no\n"); }Else{ for(inti =0; I < T; i + +) {//Check if each t has a happy            BOOLChck =false; for(intj = i; J < b; J + = t) {if(Boy[j]) Chck =true; } for(intj = i; J < G; J + = t) {if(Girl[j]) Chck =true; }if(Chck = =false) {printf("no\n"); Break; }if(i = = t1)printf("yes\n"); }    }return 0;}

codeforces515b Drazil and his Happy Friends (mathematics)

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.