牛客网多校练习赛4 D Another Distinct Values (数学规律)

链接:https://www.nowcoder.com/acm/contest/142/D
来源:牛客网

题目描述

 


Chiaki has an n x n matrix. She would like to fill each entry by -1, 0 or 1 such that r1,r2,...,rn,c1,c2, ..., cn are distinct values, where ri be the sum of the i-th row and ci be the sum of the i-th column.
输入描述:
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 200), indicating the number of test cases. For each test case:The first line contains an integer n (1 ≤ n ≤ 200) -- the dimension of the matrix.
输出描述:
For each test case, if no such matrix exists, output ``impossible'' in a single line. Otherwise, output ``possible'' in the first line. And each of the next n lines contains n integers, denoting the solution matrix. If there are multiple solutions, output any of them.

 

示例1

 

输入
复制

2
1
2

 

输出
复制

impossible
possible
1 0
1 -1

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 201
#define MAX 1000000000
using namespace std;

int n;
int mp[maxn][maxn];
/*
题目大意:给定一个矩阵的维度,
问是否能构造出一个矩阵,使得
行之和,列之和均不相同且填充矩阵用的数字只能用0,1,-1。

首先能产生的序列和为-n到n,考虑n和-n是否能同时出现,
如果同时出现则有一行全为1有一行全为-1,则这两行对所有列的贡献度为0,
明显比较奇怪。。。。我感觉不大可能,自己试了试之后发觉。

那么不妨设其有n这个和出现,
则-n不可能出现,那么考虑n-1,肯定有一列只有一个0其余全是1,
这个也不可能出现在行中,因为对列的贡献全是2 了。
由于对称性可以任意调整行列,不妨放在第一列,
那么就形成递推的结构了(其实手画到6后不自觉就会发现递归结构。。)
当n为偶数时存在,在递归画偶数时,遇到偶数则边界填1,指定位置填0,
遇到奇数则边界全是-1,如此这样。
*/

void draw(int n)
{
    if(n==0)return ;
    if(n&1)
    {
        for(int i=1;i<=n;i++)
        {
            mp[i][n]=-1;
            mp[n][i]=-1;
        }
    }
    else
    {
    for(int i=1;i<=n;i++)
        mp[n][i]=1;
       mp[n-1][n]=0;
    for(int i=n-2;i>=1;i--)
        mp[i][n]=1;
    }
    draw(n-1);
}

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n&1) puts("impossible");
        else
        {
            puts("possible");
            draw(n);
            for(int i=1;i<=n;i++)
            {
                cout<<mp[i][1];
                for(int j=2;j<=n;j++)
                    cout<<" "<<mp[i][j];
                cout<<endl;
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值