Title Link: Http://codeforces.com/problemset/problem/515/B
The topic meaning: There are n boys and M girl, with B-Boy and G-Girl (by giving an array subscript) is happy, which stipulates each round of dinner, the dispatch number for I mod n men and i mod m female go. As long as one of them is happy, the other becomes happy, asking if all men and women are eventually happy.
A step-by-step simulation is possible. There is one difficulty with this question: how many times will it take to judge that some men and women will not happy in any way. Because of the small amount of data, only 100. The worst case scenario is that every man and woman has a chance to mix, so 100 * 100 times to control the number. Each match is checked to see if each man and woman has been happy, so as to end the cycle as early as possible, do not make unnecessary combinations.
Open virtual do, found, why B than c less people do out ... (c is completely wood with ideas--)
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5 using namespacestd;6 7 Const intMAXN = -+5;8 intBOY[MAXN], GIRL[MAXN];9 intN, M;Ten One intMain () A { - #ifndef Online_judge -Freopen ("In.txt","R", stdin); the #endif //Online_judge - - intB, G; - while(SCANF ("%d%d", &n, &m)! =EOF) { +memset (Boy,0,sizeof(Boy)); -Memset (Girl,0,sizeof(Girl)); + A int inch; atscanf"%d", &b); - for(inti =0; I < b; i++) { -scanf"%d", &inch); -boy[inch] =1; - } -scanf"%d", &g); in for(inti =0; I < G; i++) { -scanf"%d", &inch); togirl[inch] =1; + } - BOOLFlag; the intSTB =0, STG =0; * intCNT = maxn*MAXN; $ while(CNT) {Panax NotoginsengFlag =true; -STB%=N; theSTG%=m; + if(Boy[stb] | | girl[stg]) {//One of them is happy. ABOY[STB] =1; theGIRL[STG] =1; + } - //Check if both sexes are happy . $ for(inti =0; I < n; i++) { $ if(!Boy[i]) { -Flag =false; - Break; the } - }Wuyi for(inti =0; I < m; i++) { the if(!Girl[i]) { -Flag =false; Wu Break; - } About } $ if(flag) - Break; -stb++; -stg++; Acnt--; + } theprintf"%s\n", Flag?"Yes":"No"); - } $ return 0; the}
Codeforces 515B. Drazil and his Happy Friends problem solving report