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

 

이번에 볼 문제는 백준 12717번 문제인 Crop Triangles (Small)이다.
문제는 아래 링크를 확인하자.

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

 

12717번: Crop Triangles (Small)

The first line of input gives the number of cases, N. N test cases follow. Each test case consists of one line containing the integers n, A, B, C, D, x0, y0 and M separated by exactly one space. n will be the number of trees in the input set

www.acmicpc.net

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

 

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

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

ll sumx = 0, sumy = 0;
ll ans = 0, tmp = 1;
ll cnt[3][3];

void func(int idx) {
	if (idx == 3) {
		if (sumx % 3 == 0 && sumy % 3 == 0) ans += tmp;
	}
	else {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (cnt[i][j] > 0) {
					sumx += i, sumy += j;
					tmp *= cnt[i][j];
					cnt[i][j]--;
					func(idx + 1);
					cnt[i][j]++;
					tmp /= cnt[i][j];
					sumx -= i, sumy -= j;
				}
			}
		}
	}
}

void solve() {
	memset(cnt, 0, sizeof(cnt));
	ans = 0;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) cnt[i][j] = 0;
	}

	ll N, A, B, C, D, x, y, M; cin >> N >> A >> B >> C >> D >> x >> y >> M;
	for (int i = 0; i < N; i++) {
		cnt[x % 3][y % 3]++;
		x = (A * x + B) % M;
		y = (C * y + D) % M;
	}

	func(0);

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

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

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

'BOJ' 카테고리의 다른 글

[BOJ 13141 // C++] Ignition  (0) 2022.05.22
[BOJ 24617 // C++] Redistributing Gifts  (0) 2022.05.22
[BOJ 12720 // C++] Number Sets (Large)  (0) 2022.05.22
[BOJ 2458 // C++] 키 순서  (0) 2022.05.22
[BOJ 1746 // C++] 단체 릴레이  (0) 2022.05.22

+ Recent posts