※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 12719번 문제인 Number Sets (Small)이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/12719 

 

12719번: Number Sets (Small)

For each test case, output one line containing the string "Case #X: Y" where X is the number of the test case, starting from 1, and Y is the number of sets.

www.acmicpc.net

12720번 문제에서 입력이 작아진 형태이다. 해당 글을 참고하여 문제를 해결하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

int uf[1001];
int findf(int x) {
	if (uf[x] == x) return x;
	return uf[x] = findf(uf[x]);
}

bool sieve[1000];

void solve() {
	ll L, R, P; cin >> L >> R >> P;
	int iterend = R - L;
	for (int i = 0; i <= iterend; i++) uf[i] = i;
	
	if (P > 999) {
		cout << R - L + 1 << '\n';
		return;
	}
	for (ll p = P; p < 1000; p++) {
		if (sieve[p]) continue;
		ll LL = L, RR = R;
		if (L % p) LL += (p - (L % p));
		RR -= R % p;
		for (ll i = LL; i < RR; i += p) {
			int x = i - L, y = RR - L;
			x = findf(x), y = findf(y);
			uf[y] = x;
		}
	}

	int ans = 0;
	for (int i = 0; i <= iterend; i++) {
		if (i == findf(i)) ans++;
	}

	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	sieve[0] = sieve[1] = 1;
	for (int i = 2; i < 32; i++) {
		if (sieve[i]) continue;
		for (int j = i * i; j < 1000; j += i) sieve[j] = 1;
	}

	int T; cin >> T;
	for (int t = 1; t <= T; t++) {
		cout << "Case #" << t << ": ";
		solve();
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 12718 // C++] Crop Triangles (Large)  (0) 2022.05.22
[BOJ 6135 // C++] Cow Hurdles  (0) 2022.05.22
[BOJ 13141 // C++] Ignition  (0) 2022.05.22
[BOJ 12721 // C++] Mousetrap (Small)  (0) 2022.05.22
[BOJ 24617 // C++] Redistributing Gifts  (0) 2022.05.22

+ Recent posts