B. Drazil and his Happy friendstime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputstan Dard 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.
The topic is very simple, is to see, can in the end make all people happy.
The first mistake, in the value of I to traverse, the second time directly let I <= 1000000, too, so I exactly how much to take, I do not know. Think about it again.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6#include <ctype.h>7 #defineMem (A, B) memset ((a), (b), (sizeof (a)))8 using namespacestd;9 Const intMax_size = the;Ten One intMain () A { - intN, M; - intBoy[max_size]; the intGirl[max_size]; -Mem (Boy,0); -Mem (Girl,0); - intBoy_num, B; + intGirl_num, G; -CIN >> N >>m; +CIN >>Boy_num; A for(inti =0; i < Boy_num; i++) at { -CIN >>b; -BOY[B] =1; - } -CIN >>Girl_num; - for(inti =0; i < Girl_num; i++) in { -CIN >>G; toGIRL[G] =1; + } - the for(inti =0; I <=1000000; i++) * { $b = i%N;Panax Notoginsengg = i%m; - if(boy[b]+Girl[g]) the { +BOY[B] =1; AGIRL[G] =1; the } + } - BOOLTag =true; $ for(inti =0; I < n; i++) $ { - if(Boy[i] = =0) -Tag =false; the - }Wuyi for(inti =0; I < m; i++) the { - if(Girl[i] = =0) WuTag =false; - } About if(Tag = =false) $cout <<"No"<<Endl; - Else -cout <<"Yes"<<Endl; - return 0; A}
View Code
Codeforces 515B. Drazil and his Happy Friends