思路:
我们直接枚举n,然后算出m,然后x,y也可以相应算出,然后构造矩阵判断就行了
c o d e code code
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAXN = 2e6 + 10;
int t, maxx;
int a[MAXN], b[MAXN];
int abs(int x) {
if (x < 0)
return -x;
return x;
}
int main() {
scanf("%d", &t);
for (int i = 1, q = 0; i <= t; i++) {
scanf("%d", &q);
a[q]++;
}
int x = 0;
for (int i = 1; i <= t; i++)
if (a[i] != i * 4) {
x = i;
break;
}
for (int i = 1; i <= t; i++)
if (a[i] != 0)
maxx = i;
for (int i = 1; i * i <= t; i++) {
if (t % i == 0) {
int n = i;
int m = t / n;
int y = n + m - x - maxx;
if (abs(n - x) + abs(m - y) != maxx)
continue;
for (int j = 0; j <= n + m; j++) b[j] = 0;
for (int i1 = 1; i1 <= n; i1++)
for (int j1 = 1; j1 <= m; j1++) b[abs(i1 - x) + abs(j1 - y)]++;
bool flag = 0;
for (int j = 0; j <= n + m; j++)
if (b[j] != a[j]) {
flag = 1;
break;
}
if (!flag) {
printf("%d %d\n%d %d", n, m, x, y);
return 0;
}
}
}
for (int i = 1; i * i <= t; i++) {
if (t % i == 0) {
int m = i;
int n = t / m;
int y = n + m - x - maxx;
if (abs(n - x) + abs(m - y) != maxx)
continue;
for (int j = 0; j <= n + m; j++) b[j] = 0;
for (int i1 = 1; i1 <= n; i1++)
for (int j1 = 1; j1 <= m; j1++) b[abs(i1 - x) + abs(j1 - y)]++;
bool flag = 0;
for (int j = 0; j <= n + m; j++)
if (b[j] != a[j]) {
flag = 1;
break;
}
if (!flag) {
printf("%d %d\n%d %d", n, m, x, y);
return 0;
}
}
}
printf("-1");
return 0;
}