515B. Drazil and His Happy Friends

来源:互联网 发布:剑灵灵女最美捏脸数据 编辑:程序博客网 时间:2024/05/21 22:34
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 andm (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 followb 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 followg 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 ofi 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个女生
    第二行:输入happy的男生数,后面跟随happy的男生坐标
    第三行:输入happy的女生数,后面跟随happy的女生坐标。

 有一个i,从0开始增加,表示第几天,选出第i mod n个男生和 第i mod m个女生坐在一起吃饭,他们中有一个happy就会感染另一个人,使他们两个人都happy。反之,保持原来的心情。
 问:能否让所有学生都happy,能则输出yes,不能则输出no。(这里i可以无限递增下去)。

分析:开始我想这肯定是一个模拟题。但是i递增到多少可以认为是no呢?i是可以无线递增下去的,所以模拟是行不通的。
    其实这道题是最小公约数的问题。首先求m和n的最小公约数g,如果g以内的男生女生数全都happy,一定可以传染到g以外的人。那g以内的人,只要有一个人happy,就一定在一轮以内传染给另一个人,是两个人都happy。。最小公约数还是有点忙博大精深滴啊。

         以上,就省去了i的问题,不论i是多少,只考虑能不能达到两个人都happy。 我定义一个数组,表示两个人都happy的坐标,只有两个人都happy,数组此位的值才为1。每次输入只要有一个性别的为1,就令此位对g的最小公约数那位为1。输入完,遍历数组在g以内的若全为1,就是yes,不呢个就为no。

#include<iostream>#include<cstdio>#include<cstring>#include<ctime>#include<algorithm>#include <map>#include <queue>#include <set>using namespace std;//515B  最大公约数int n,m,b,a[1000];int main(){    cin>>n>>m;    int g=__gcd(n,m);    for(int i=0;i<2;i++)    {        cin>>b;        for(int j=0;j<b;j++)        {            int q;            cin>>q;            a[q%g]=1;        }    }    for(int i=0;i<g;i++)    {        if(a[i]==0)        {            cout<<"No";            return 0;        }    }    cout<<"Yes";    return 0;}


0 0
原创粉丝点击