牛客网暑期ACM多校训练营(第四场)--- D题

D

Another Distinct Values

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
Special Judge, 64bit IO Format: %lld

题目描述 

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

 



思路:神塔么直接输出!!找维度是3的矩阵两个小时...大胆猜测不存在然后继续大胆---奇数维度不存在,偶数维度存在(没错就是瞎**猜),再然后被强大的队友搞出来维度是4的矩阵,再大胆猜规律(哭了)

4*4

1111
0-1-1-1
1110
00-1-1

6*6

111111
0-1-1-1-1-1
111110
00-1-1-1-1
111100
000-1-1-1

依次类推......

 


代码: 

#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin >> n;
        {
            if(n%2==1)
                puts("impossible");
            else
            {
                puts("possible");
                for(int i=0; i<n; i++)
                {
                    if(i!=0)
                        printf(" ");
                    printf("1");
                }
                printf("\n");
                int cnt=1;
                for(int i=1; i<n-1; i++)
                {
                    if(i%2==0)
                    {
                        for(int j=0; j<n-cnt; j++)
                        {
                            printf("1 ");
                        }
                        for(int j=n-cnt; j<n; j++)
                        {
                            if(j!=n-cnt)
                                printf(" ");
                            printf("0");
                        }
                        cnt++;
                    }
                    else
                    {
                        for(int j=0; j<cnt; j++)
                        {
                            printf("0 ");
                        }
                        for(int j=cnt; j<n; j++)
                        {
                            if(j!=cnt)
                                printf(" ");
                            printf("-1");
                        }
                    }
                    printf("\n");
                }
                for(int i=0; i<n/2; i++)
                {
                    printf("0 ");
                }
                for(int i=0; i<n/2; i++)
                {
                    if(i!=0)
                        printf(" ");
                    printf("-1");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

欢迎指正与交流

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值