CF515B Drazil and His Happy Friends题解

CF515B Drazil and His Happy Friends

Question : There are n boys and m girls. On the t day, the t%n boys and t%m girls are invited to have dinner. Some people are optimistic, and eating with optimistic people can become optimistic. Ask a few days later whether all people can become optimistic.

Solution : Direct violence is fine. When I first thought about it, it was WA. Later, I thought that if the people behind are optimistic, they can’t update to the previous ones only by looping n*m times, so choose to loop multiple times. In the future, small data problems must be Circulation to the end duck 0.0

Code :

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 110;
int n, m, nn, mm;
int a[N], b[N];
int tu[N][N];
int va[N], vb[N];
int main() {
    
    
	int x;
	scanf("%d%d", &n, &m);
	scanf("%d", &nn); while(nn--) scanf("%d", &x), a[x] = 1;
	scanf("%d", &mm); while(mm--) scanf("%d", &x), b[x] = 1;
	int t = 0;
	for(int t = 0; t <= n*m*2; ++t)  //就是这里!!
		if(a[t%n] || b[t%m]) a[t%n] = b[t%m] = 1;
	int f = 1;
	for(int i = 0; i < n; ++i) if(!a[i]) {
    
     f = 0; break; }
	for(int i = 0; i < m; ++i) if(!b[i]) {
    
     f = 0; break; }
	if(f) puts("Yes");
	else puts("No");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43408978/article/details/109005575