B. Drazil and his Happy Friends

Source: Internet
Author: User

This is a codeforces Round #292 (Div. 2), the original question here, test instructions is:

Xiao Ming has n male classmates (numbered 0 ~ n-1) and M female classmates (numbered 0 ~ m-1), Xiao Ming to arrange dating between men and women, how to arrange a date, if the time is I (I >=0), Xiao Ming in I day invited I% n male classmate, and I% m female classmate date, if men and women There is a happy between the students, then two people are happy, then the question came, can Xiao Ming make them all happy?

I was thinking, if N and M are equal, then if there is happiness between the numbering range [0, n], for example,

Example A:

if n = m = 3, n happy with 0, 1, M happy 2, then all people will be happy after the date.

What if N and M don't want to wait? The question comes, and how does this time relate?

I couldn't think of the process of simulating this appointment, using O (n * m) method, the result was hack, the water burst = =, after seeing the editorial only then understand.

OK, back to the question, if N and M do not want to wait, assuming n = 2, M = 3, then after the time I = 2, in the boys 2%2 = No. 0 Boys and 2%3 = 2nd girls Dating, you can put the M 2nd girls as A, and two boys date, the problem becomes n = 2, M = 1, the reason to do this is to find an interval, this interval satisfies, each number is happy, just like in the previous example a. Then continue, n = 2, M = 1 o'clock, at time I = 1 o'clock, boys 1 2 = 1th Boys and 1 1 = No. 0 Girls Dating, the N of the 1th Boys as a, and 1 girls dating, the problem is programmed n = m = 1, so that the previous said a purpose , find the interval [0, 1) if the corresponding number in this one is happy, then all people will be happy. First set C is N and M of the greatest common divisor, at n = 2, M = 3, I = 2 o'clock, so transform, n = 2, m = m% n = 1, I also do this when accumulating, and finally change the problem to n = m = 1, so that as long as the interval between

[0, 1] The corresponding number of people are happy, then all people will be happy. is not very like GCD, in example A, because 0, 1th is happy, then 0 1 = 0, 1 1 = 1, will be happy because of them, so, each happy person's number to modulo N and M of the convention number, to find out because they are happy with the number of people. Well, that's a lot of logic, that's the code.

#include <stdio.h> #include <iostream> #include <map> #include <string.h>using namespace std;  int gcd (int a, int b) {    int c, D;    D = Max (A, b); c = min (a, b);    if (d%c = = 0) return c;    else return gcd (d%c, c);} int Pep[101];int Main () {    int n, m, B, G, C, I, CNT = 0, id;    Freopen ("E:/in.txt", "R", stdin);    memset (PEP, 0, sizeof (PEP));    CIN >> n >> m;    c = gcd (n, m);    CIN >> B;    for (i = 0; i < b; i++) {    cin >> ID;    if (!pep[id%c]) pep[id%c] = 1, cnt++;     }    Cin >> G;    for (i = 0; i < g; i++) {    cin >> ID;    if (!pep[id%c]) pep[id%c] = 1, cnt++;    }        if (cnt = = c) cout << "Yes";    else cout << "No";    return 0;}


B. Drazil and his Happy Friends

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.