【codeforces 515B】Drazil and His Happy Friends

[Title link] http://codeforces.com/contest/515/problem/B

[title]

On the first day, i%n boys and i%m girls were chosen to have dinner together.
As long as one of the couples is happy, the other can become happy.
And after being happy, I have been happy.
Give you n boys, m girls status (happy or not);
Ask if everyone will be happy someday.

[Abstract]

The i%n and i%m loops must have loop nodes.
Find the length of the loop section.
Every time I try to use the above method in the cycle festival, see if there is any way to make some people from unhappy to happy;
If possible, repeat the cyclic land saving step.
Until no one is happy with the operation of this cycle section.

[Number Of WA]

0

[Complete code]

#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[N], gh[N];
int n, m,b,g;
bool bo[N][N];

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;
}

Posted by thangappan on Sun, 10 Feb 2019 20:06:17 -0800