題目
分析
這種構造題除了流毒我也不想說什麼。
題解也是直接給了one of the possible solutions
,我能做的就是證明這種solution
是對的,,,
首先,只有時無解,否則可以構造以下方陣():
文字敘述我不想打了,英文很簡單可以自己看:
- In the first row, we write down the numbers .
- Each following row but the last is obtained by adding to each number from
the previous row.- The last row is obtained in the following way:
For each column, if the numbers are the numbers written in that column so far, we write the number 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]);
}