B. Drazil and His Happy Friends
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are 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 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 ≤ 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 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 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 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.
n个男孩,m个女孩,假如他们之间约会无限次,我们就会发现约会分成了gcd(m,n)组。只要每组有至少1人快乐,那么在有限的步数下,整个组都会快乐,于是,我们只要分好组,然后找组内是否有人快乐。
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> 10 #define LL long long 11 #define PF(x) ((x)*(x)) 12 #define LF(x) ((x)*PF(x)) 13 14 using namespace std; 15 const int INF=1<<31-1; 16 const int max9=1e9; 17 const int max6=1e6; 18 const int max3=1e3; 19 20 int gcd(int a,int b) 21 { 22 return b==0?a:gcd(b,a%b); 23 } 24 int t1[120]; 25 int t2[120]; 26 27 int main() 28 { 29 int n,m; 30 while(cin >> n >> m) 31 { 32 memset(t1,0,sizeof(t1)); 33 memset(t2,0,sizeof(t2)); 34 int x; 35 int temp; 36 cin >> x; 37 while(x--) 38 { 39 cin >> temp; 40 t1[temp]=1; 41 } 42 cin >> x; 43 while(x--) 44 { 45 cin >> temp; 46 t2[temp]=1; 47 } 48 int w=gcd(n,m); 49 int s=n/w*m; 50 int flag; 51 for(int i=0;i<w;i++) 52 { 53 flag=0; 54 for(int j=i;j<s;j+=w) 55 { 56 if(t1[j%n]) flag=1; 57 if(t2[j%m]) flag=1; 58 if(flag) break; 59 } 60 if(flag==0) break; 61 } 62 if(flag) cout << "Yes" << endl; 63 else cout << "No" << endl; 64 } 65 return 0; 66 }