COCI2016/2017 Round2T4 Prosjecni

本文探讨了在特定条件下构造矩阵的方法,详细介绍了除n=2情况外的矩阵构造方案,通过逐步添加常数和调整最后一行元素,确保每行、每列的平均数符合特定规律。文章提供了算法实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

题目

题目

分析

这种构造题除了流毒我也不想说什么。
题解也是直接给了one of the possible solutions,我能做的就是证明这种solution是对的,,,


首先,只有 n = 2 n=2 n=2时无解,否则可以构造以下方阵( n ≥ 1 n\geq 1 n1):
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(n1)1+n(n1)1+2n2n(n1)n[1+2n2n(n1)]i=1n1[1+2i1n(n1)]22+2n(n1)2+n(n1)2+2n2n(n1)n[2+2n2n(n1)]i=1n1[2+2i1n(n1)]n1n1+2n(n1)n1+n(n1)n1+2n2n(n1)n[n1+2n2n(n1)]i=1n1[n1+2i1n(n1)]21n(n1)n(n1)23n(n1)2n1n(n1)n[2n1n(n1)]i=1n1[2in(n1)]

文字叙述我不想打了,英文很简单可以自己看:

  • 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,...,n1,2n(n1).
  • Each following row but the last is obtained by adding n ( n − 1 ) 2 \dfrac{n(n-1)}{2} 2n(n1) 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,...,an1 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}) nan1(a1+a2+...+an1) 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]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值