Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.
Test instructions: A total of n boys and M girls, from the 0~n-1he0~m-1 number, in the two who are happy, according to I%n and i%m to take the number of one happy is another happy, judge whether to make everyone happy.
Analysis: Violence, according to data, even if the maximum number of times per return will not exceed the time of 10,000.
1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 7 intMain ()8 {9 inta[ $],b[ $];Ten intn,m; OneCin>>n>>m; AMemset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); - the intnum; -Cin>>num; - for(intI=0; i<num;i++) {intp;cin>>p;a[p]=1;} -Cin>>num; + for(intI=0; i<num;i++) {intp;cin>>p;b[p]=1;} - + for(intI=0;i<10000; i++) A if(a[i%n]| | B[I%M]) a[i%n]=b[i%m]=1; at intflag=1; - for(intI=0; i<n| | i<m;i++) - { - if(i<n&&!a[i%n]) {flag=0; Break;} - if(I<m&&!b[i%m]) {flag=0; Break;} - } in if(flag) cout<<"Yes"<<Endl; - Elsecout<<"No"<<Endl; to}
B. Drazil and his Happy Friends