codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

515B.md (2898B)


      1 Source: https://codeforces.com/problemset/problem/515/B
      2 
      3 B. Drazil and His Happy Friends
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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 ![](https://espresso.codeforces.com/4b8097bd0e14e104f0d3ee4c567d8dae1d841e28.png)-th boy and ![](https://espresso.codeforces.com/affc1977a5192853063fd8e722c851c3e5503c85.png)-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.
     24 
     25 Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
     26 
     27 Input
     28 
     29 The first line contains two integer *n* and *m* (1 ≤ *n*, *m* ≤ 100).
     30 
     31 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 *x*1, *x*2, ..., *x**b* (0 ≤ *x**i* < *n*), denoting the list of indices of happy boys.
     32 
     33 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 *y*1, *y*2, ... , *y**g* (0 ≤ *y**j* < *m*), denoting the list of indices of happy girls.
     34 
     35 It is guaranteed that there is at least one person that is unhappy among his friends.
     36 
     37 Output
     38 
     39 If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 2 3  
     47 0  
     48 1 0
     49 ```
     50 
     51 Output
     52 
     53 ```
     54 Yes
     55 ```
     56 
     57 Input
     58 
     59 ```
     60 2 4  
     61 1 0  
     62 1 2
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 No
     69 ```
     70 
     71 Input
     72 
     73 ```
     74 2 3  
     75 1 0  
     76 1 1
     77 ```
     78 
     79 Output
     80 
     81 ```
     82 Yes
     83 ```
     84 
     85 Note
     86 
     87 By ![](https://espresso.codeforces.com/169ade208e6eb4f9263c57aaff716529d59c3288.png) we define the remainder of integer division of *i* by *k*.
     88 
     89 In first sample case:
     90 
     91 * 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.
     92 * On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
     93 * 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.
     94 * 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.
     95 * 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.