QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#522156#8485. Magic SquareSTnofarjo#RE 0ms3576kbC++141.3kb2024-08-16 19:15:232024-08-16 19:15:25

Judging History

This is the latest submission verdict.

  • [2024-08-16 19:15:25]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 3576kb
  • [2024-08-16 19:15:23]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

void solve() {
	int n;
	cin >> n;
	vector<vector<long long>> a(n, vector<long long>(n));
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			cin >> a[i][j];
		}
	}

	long long sqr = n * n;
	long long real_sum = sqr * (sqr+1) / 2 / n;

	vector<int> ai, aj;
	for (int i=0; i<n; i++) {
		long long sum = 0;
		for (int j=0; j<n; j++) sum += a[i][j];
		if (sum != real_sum) ai.push_back(i); 
	}
	for (int i=0; i<n; i++) {
		long long sum = 0;
		for (int j=0; j<n; j++) sum += a[j][i];
		if (sum != real_sum) aj.push_back(i); 
	}

	for (int i=0; i<2; i++) {
		swap(a[ai[0]][aj[0]], a[ai[1]][aj[1]]);

		bool ok = true;
		for (int r=0; r<n && ok; r++) {
			long long sum = 0;
			for (int c=0; c<n && ok; c++) sum += a[r][c];
			if (sum != real_sum) ok = false;
		}
		for (int c=0; c<n && ok; c++) {
			long long sum = 0;
			for (int r=0; r<n && ok; r++) sum += a[r][c];
			if (sum != real_sum) ok = false;
		}

		if (ok) {
			cout << ai[0] + 1 << ' ' << aj[0] + 1 << '\n' << ai[1] + 1 << ' ' << aj[1] + 1 << '\n';
			return;
		}

		swap(a[ai[0]][aj[0]], a[ai[1]][aj[1]]);
		swap(aj[0], aj[1]);
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int tcs = 1;
	// cin >> tcs;
	while (tcs--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

3
6 9 2
3 5 7
8 1 4

output:

1 1
3 3

result:

ok OK

Test #2:

score: -100
Runtime Error

input:

4
16 3 2 13
5 10 11 8
9 6 7 12
1 15 14 4

output:


result: