CodeForces515B Drazil and His Happy Friends (Mathematics), happytreefriends

Source: Internet
Author: User
Tags integer division

CodeForces515B Drazil and His Happy Friends (Mathematics), happytreefriends

B. Drazil and His Happy Friends
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Drazil has too friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he has Ted the following plan.

There are n boys and m girls among his friends. let's number them from 0 to n then-then 1 and 0 to m then-else 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 rows ≤ limit n, interval m ≤ limit 100 ).

The second line contains integer B (0 rows ≤1_ B rows ≤1_n), denoting the number of happy boys among friends of Drazil, and then follow B distinct integers x1, between x2, else ..., Partition xb (0 partition ≤ partition xi partition <partition n), denoting the list of indices of happy boys.

The third line conatins integer g (0 records ≤0000g records ≤0000m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, 1_y2, else... , Policyg (0 records ≤ policyj records <Symbol 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.

Description
There are some boys and girls, some of which are happy. The main character starts from boys' 0 and female 0. Each time he calls a meal at home = If one of them is happy, both of them will be happy, otherwise they will all go home and ask the final protagonist to make are everyone happy.

Solutions
Because the data is small, it is also possible to simulate brute force attacks.
You can also find the number of boys and girls in gcd. If gcd is set to 1, there is a Happy ending for everyone. If gcd! = 1 If gcd = t, then it is okay to bind t people together as a person. If you want to check whether at least one of every t people's I numbers is happy in all groups. If no one is happy on the I, then surely no one is happy at the end of the I =

The Code is as follows:

# Include <cstdio> const int maxn = 110; int boy [maxn]; int girl [maxn]; int gcd (int a, int B) {return B = 0? A: gcd (B, a % B) ;}int main () {int B, g; scanf ("% d", & B, & g ); int cnt; scanf ("% d", & cnt); int chk1 = cnt; while (cnt --) {int a; scanf ("% d", & ); boy [a] = 1;} scanf ("% d", & cnt); int chk2 = cnt; while (cnt --) {int a; scanf ("% d ", & a); girl [a] = 1;} int t = gcd (B, g); if (t = 1) {if (chk1 | chk2) printf ("Yes \ n"); else printf ("No \ n");} else {for (int I = 0; I <t; I ++) {// check whether there is a happy bool chck = false in each t; for (int j = I; j <B; j + = t) {if (boy [j]) chck = true ;}for (int j = I; j <g; j + = t) {if (girl [j]) chck = true;} if (chck = false) {printf ("No \ n"); break;} if (I = t-1) printf ("Yes \ n") ;}} return 0 ;}

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.