codeforces 515B.Drazil and His Happy Friends

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 }

时间: 2024-10-29 10:46:28

codeforces 515B.Drazil and His Happy Friends的相关文章

codeforces 515B. Drazil and His Happy Friends 解题报告

题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl (通过给出数组下标)是 happy的,规定每轮 dinner 中,派出编号为 i mod n 个男 和 i mod m 个女去.只要他们其中一个为 happy 时,另一个也会变得 happy,问最终所有男女是否都变得 happy. 一步一步模拟就可以了.这个问题有一个难点,就是究竟要进行多少次才能判断

Codeforces 515C. Drazil and Factorial

//codeforces 515C. Drazil and Factorial #include <iostream> #include <cstring> #include <cstdio> using namespace std; /** 4!=2!*2!*3! 6!=1*2*3*4*5*6=5!*3! 8!=1*2*3*4*5*6*7*8=7!*2!*2!*2! 9!=1*2*3*4*5*6*7*8*9=7!*2!*3!*3! */ int main() { ch

codeforces 515C. Drazil and Factorial 解题报告

题目链接:http://codeforces.com/problemset/problem/515/C 题目意思:给出含有 n 个只有阿拉伯数字的字符串a(可能会有前导0),设定函数F(a) = 每个数字的阶乘乘积.例如 F(135) = 1! * 3! * 5! .需要找出 x,使得F(x) = F(a),且组成 x 的数字中没有0和1.求最大的 x 为多少. 这个我是看了每个数字的转换才知道怎么做的. 0, 1     —— >  empty(用空串表示) 2         —— > 

codeforces 515A.Drazil and Date 解题报告

题目链接:http://codeforces.com/problemset/problem/515/A 题目意思:问能否从 (0, 0) 出发,恰好走 s 步,到达该位置(a, b). 首先容易知道,最少的步数为 |a| + |b|,最长就是——当然是无限啦(乱兜圈子就行).然后就有一点点带着个人感觉来做了= =,第六感确实不错的,呵呵,证明就有待考究了. 如果 |a| + |b| 跟 s 的奇偶性相同就有解,否则无解.当然前提是满足 s 至少要比最少的步数大! 1 #include <iost

Codeforces 516D Drazil and Morning Exercise (栈、二分)

题目链接 https://codeforces.com/contest/516/problem/D 题解 我还是数据结构水平太低了啊--连一个点子树内距离不超过\(l\)的点数都不会求 首先有一个熟知的结论是,我们任取原树的一条直径,那么对于任何一个点,直径的两端点中至少有一个到它的距离等于它到所有点的最远距离. 假设直径是\((u_d,v_d)\), 那么我们就把\(u\)的最远距离的式子化简成了\(f_u=\max(dis(u,u_d),dis(u,v_d)\). 考虑\(u_d\)和\(v

Codeforces 515D Drazil and Tiles

题意:给你一个图,包含空格 . 和墙* , 你现在要在空格处放置1x2的格子使得空白处全部填满,问你是否是 (有解或者多个解) 还是(只有一个解). 解题思路:这个题图太大了,显然不能用DLX或者状压dp,应该怎么样能,我们知道 如果一个空白边上三面都是墙或者已经填被填上了,那这个空白只有一种填法, 然后根据这个性质去进行搜索就能个得到是否只有一个解了. 解题代码: 1 // File Name: b.cpp 2 // Author: darkdream 3 // Created Time: 2

Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)

题目地址:codeforces 292 B 用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列.若最后还有为"."的,说明无解或解不唯一. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似topsort,'.'与上下左右的'.',的相连.从度为1的点作为突破口. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostr

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie