链接:https://www.nowcoder.com/acm/contest/142/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
方法:规律
2*2
1 1
0 -1
4*4
1 1
0 -1
1 1
0 -1
1 1 1 1
0 -1 -1 -1
1 -1 1 1
1 -1 0 -1
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
int main()
{
int n,i,j,a[205][205],t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n%2)
{
printf("impossible\n");
}
else
{
printf("possible\n");
for(i=1;i<=n;i++)
{
if(i%2)
a[i][i]=1;
else
a[i][i]=-1;
}
for(i=2;i<=n;i++)
{
if(i%2==0)
{
a[i][i-1]=0;
}
else
a[i][i-1]=-1;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(i%2)
a[i][j]=1;
else
a[i][j]=-1;
}
}
for(j=1;j<=n;j++)
{
for(i=j+2;i<=n;i++)
{
if(j%2)
a[i][j]=1;
else
a[i][j]=-1;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][j]);
}
}
}
}