您的位置:首页 > 移动开发

【codeforces 515B】Drazil and His Happy Friends

2017-10-04 18:44 501 查看

【题目链接】:http://codeforces.com/contest/515/problem/B

【题意】

第i天选择第i%n个男生,第i%m个女生,让他们一起去吃饭;
只要这一对中有一个人是开心状态,另外一个人也能变成开心状态;
且开心之后就一直开心了;
给你n个男生,m个女生的状态(是否开心);
问你是否到了某一天所有人都会变的开心;

【题解】

那个i%n和i%m的循环肯定有循环节的;
找到那个循环节的长度;
每次在循环节内尝试用上述办法,看看有没有办法让某些人从不开心变为开心;
如果可以的话;就重复这个循环节地步骤;
直到没有一个人因为这个循环节的操作变成开心状态为止。

【Number Of WA】

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

bool bh
, gh
;
int n, m,b,g;
bool bo

;

int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(m);
rei(b);
rep1(i, 1, b)
{
int x;
rei(x);
bh[x] = true;
}
rei(g);
rep1(i, 1, g)
{
int x;
rei(x);
gh[x] = true;
}
int len = 0;
for (int i = 0;; i++)
{
int x = i%n, y = i%m;
if (bo[x][y])
{
len = i - 1;
break;
}
bo[x][y] = true;
}
bool ok = true;
while (ok)
{
ok = false;
for (int i = 0; i <= len; i++)
{
int x = i%n, y = i%m;
if (!bh[x] || !gh[y])
{
if (bh[x] || gh[y])
{
bh[x] = gh[y] = true;
ok = true;
}
}
}
}
rep1(i, 0, n - 1)
if (!bh[i])
return puts("No"), 0;
rep1(i, 0, m - 1)
if (!gh[i])
return puts("No"), 0;
puts("Yes");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: