B. Drazil and his Happy Friends
Time limit per test2 seconds
memory limit per testMegabytes
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 a Mong Friends of Drazil, and then follow b distinct integers x1, x2,. .., xb (0≤ xi < n), denoting the list of Indice S 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, ..., y G (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.
N Boys, M-girls, if they have an unlimited number of dates, we'll find that the date is divided into the GCD (m,n) group. As long as each group has at least 1 people happy, then in a limited number of steps, the whole group will be happy, so we just need to split the group, and then find whether someone in the group is happy.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <iomanip>5#include <cctype>6#include <string>7#include <cmath>8#include <cstdio>9#include <cstdlib>Ten #defineLL Long Long One #definePF (x) ((x) * (x)) A #defineLF (x) ((x) *PF (x)) - - using namespacestd; the Const intinf=1<< to-1; - Const intmax9=1e9; - Const intmax6=1e6; - Const intmax3=1e3; + - intgcdintAintb) + { A returnb==0? A:GCD (b,a%b); at } - intt1[ -]; - intt2[ -]; - - intMain () - { in intn,m; - while(Cin >> N >>m) to { +memset (T1,0,sizeof(t1)); -memset (T2,0,sizeof(T2)); the intx; * inttemp; $CIN >>x;Panax Notoginseng while(x--) - { theCIN >>temp; +t1[temp]=1; A } theCIN >>x; + while(x--) - { $CIN >>temp; $t2[temp]=1; - } - intw=gcd (n,m); the ints=n/w*m; - intFlag;Wuyi for(intI=0; i<w;i++) the { -flag=0; Wu for(intj=i;j<s;j+=W) - { About if(t1[j%n]) flag=1; $ if(T2[j%m]) flag=1; - if(flag) Break; - } - if(flag==0) Break; A } + if(flag) cout <<"Yes"<<Endl; the Elsecout <<"No"<<Endl; - } $ return 0; the}View Code
Codeforces 515B. Drazil and his Happy Friends