COCI2016/2017 Round2T4 Prosjecni

文章目錄

題目

題目

分析

這種構造題除了流毒我也不想說什麼。
題解也是直接給了one of the possible solutions,我能做的就是證明這種solution是對的,,,


首先,只有n=2n=2時無解,否則可以構造以下方陣(n1n\geq 1):
12n112n(n1)1+n(n1)22+n(n1)2n1+n(n1)2n(n1)1+n(n1)2+n(n1)n1+n(n1)32n(n1)1+n22n(n1)2+n22n(n1)n1+n22n(n1)n12n(n1)n[1+n22n(n1)]i=1n1[1+i12n(n1)]n[2+n22n(n1)]i=1n1[2+i12n(n1)]n[n1+n22n(n1)]i=1n1[n1+i12n(n1)]n[n12n(n1)]i=1n1[i2n(n1)] \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}

文字敘述我不想打了,英文很簡單可以自己看:

  • In the first row, we write down the numbers 1,2,...,n1,n(n1)21,2,...,n - 1,\dfrac{n(n-1)}{2}.
  • Each following row but the last is obtained by adding n(n1)2\dfrac{n(n-1)}{2} to each number from
    the previous row.
  • The last row is obtained in the following way:
    For each column, if the numbers a1,...,an1a_1,...,a_{n-1} are the numbers written in that column so far, we write the number nan1(a1+a2+...+an1)na_{n-1}-(a_1 + a_2+ ... + a_{n-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]);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章