题目
分析
这种构造题除了流毒我也不想说什么。
题解也是直接给了one of the possible solutions
,我能做的就是证明这种solution
是对的,,,
首先,只有
n
=
2
n=2
n=2时无解,否则可以构造以下方阵(
n
≥
1
n\geq 1
n≥1):
1
2
⋯
n
−
1
1
2
n
(
n
−
1
)
1
+
n
(
n
−
1
)
2
2
+
n
(
n
−
1
)
2
⋯
n
−
1
+
n
(
n
−
1
)
2
n
(
n
−
1
)
1
+
n
(
n
−
1
)
2
+
n
(
n
−
1
)
⋯
n
−
1
+
n
(
n
−
1
)
3
2
n
(
n
−
1
)
⋮
⋮
⋱
⋮
⋮
1
+
n
−
2
2
n
(
n
−
1
)
2
+
n
−
2
2
n
(
n
−
1
)
⋯
n
−
1
+
n
−
2
2
n
(
n
−
1
)
n
−
1
2
n
(
n
−
1
)
n
[
1
+
n
−
2
2
n
(
n
−
1
)
]
−
∑
i
=
1
n
−
1
[
1
+
i
−
1
2
n
(
n
−
1
)
]
n
[
2
+
n
−
2
2
n
(
n
−
1
)
]
−
∑
i
=
1
n
−
1
[
2
+
i
−
1
2
n
(
n
−
1
)
]
⋯
n
[
n
−
1
+
n
−
2
2
n
(
n
−
1
)
]
−
∑
i
=
1
n
−
1
[
n
−
1
+
i
−
1
2
n
(
n
−
1
)
]
n
[
n
−
1
2
n
(
n
−
1
)
]
−
∑
i
=
1
n
−
1
[
i
2
n
(
n
−
1
)
]
\begin{matrix} 1 & 2 & \cdots & n-1 & \dfrac{1}{2}n(n-1)\\\\ 1+\dfrac{n(n-1)}{2} & 2+\dfrac{n(n-1)}{2} & \cdots & n-1+\dfrac{n(n-1)}{2} & n(n-1)\\\\ 1+n(n-1) & 2+n(n-1) & \cdots & n-1+n(n-1) & \dfrac{3}{2}n(n-1)\\\\ \vdots & \vdots & \ddots & \vdots & \vdots\\\\ 1+\dfrac{n-2}{2}n(n-1) & 2+\dfrac{n-2}{2}n(n-1) & \cdots & n-1+\dfrac{n-2}{2}n(n-1) & \dfrac{n-1}{2}n(n-1)\\\\ n\left[1+\dfrac{n-2}{2}n(n-1)\right]-\sum\limits_{i=1}^{n-1}\left[1+\dfrac{i-1}{2}n(n-1)\right] & n\left[2+\dfrac{n-2}{2}n(n-1)\right]- \sum\limits_{i=1}^{n-1}\left[2+\dfrac{i-1}{2}n(n-1)\right] & \cdots & n\left[n-1+\dfrac{n-2}{2}n(n-1)\right]- \sum\limits_{i=1}^{n-1}\left[n-1+\dfrac{i-1}{2}n(n-1)\right] & n\left[\dfrac{n-1}{2}n(n-1)\right]- \sum\limits_{i=1}^{n-1}\left[\dfrac{i}{2}n(n-1)\right] \end{matrix}
11+2n(n−1)1+n(n−1)⋮1+2n−2n(n−1)n[1+2n−2n(n−1)]−i=1∑n−1[1+2i−1n(n−1)]22+2n(n−1)2+n(n−1)⋮2+2n−2n(n−1)n[2+2n−2n(n−1)]−i=1∑n−1[2+2i−1n(n−1)]⋯⋯⋯⋱⋯⋯n−1n−1+2n(n−1)n−1+n(n−1)⋮n−1+2n−2n(n−1)n[n−1+2n−2n(n−1)]−i=1∑n−1[n−1+2i−1n(n−1)]21n(n−1)n(n−1)23n(n−1)⋮2n−1n(n−1)n[2n−1n(n−1)]−i=1∑n−1[2in(n−1)]
文字叙述我不想打了,英文很简单可以自己看:
- In the first row, we write down the numbers 1 , 2 , . . . , n − 1 , n ( n − 1 ) 2 1,2,...,n - 1,\dfrac{n(n-1)}{2} 1,2,...,n−1,2n(n−1).
- Each following row but the last is obtained by adding n ( n − 1 ) 2 \dfrac{n(n-1)}{2} 2n(n−1) to each number from
the previous row.- The last row is obtained in the following way:
For each column, if the numbers a 1 , . . . , a n − 1 a_1,...,a_{n-1} a1,...,an−1 are the numbers written in that column so far, we write the number n a n − 1 − ( a 1 + a 2 + . . . + a n − 1 ) na_{n-1}-(a_1 + a_2+ ... + a_{n-1}) nan−1−(a1+a2+...+an−1) in the nth row in that column. By doing this, we achieved that the average of that column is equal to the next to last number in the column.
计算可得除了最后一行,每行的平均数是最后一个。
计算可得最后一行的平均数是这行的倒数第二个。
计算可得每列的平均数是这列的倒数第二个。
代码
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*f;
}
#define MAXN 100
int N;
int Ans[MAXN+5][MAXN+5];
int main(){
freopen("prosjecni.in" ,"r", stdin);
freopen("prosjecni.out","w",stdout);
N=read();
if(N==2)
puts("-1");
for(int i=1;i<=N-1;i++)
Ans[1][i]=i;
Ans[1][N]=N*(N-1)/2;
for(int i=2;i<=N-1;i++)
for(int j=1;j<=N;j++)
Ans[i][j]=Ans[i-1][j]+N*(N-1)/2;
for(int i=1;i<=N;i++){
int Sum=0;
for(int j=1;j<=N-1;j++)
Sum+=Ans[j][i];
Ans[N][i]=N*Ans[N-1][i]-Sum;
}
for(int i=1;i<=N;i++,puts(""))
for(int j=1;j<=N;j++)
printf("%d ",Ans[i][j]);
}