链接: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]);
            }
        }
    }
}

Another Distinct Values相关推荐

  1. pandas使用groupby函数和agg函数获取每个分组特定变量独特值的个数(number of distinct values in each group in dataframe)

    pandas使用groupby函数和agg函数获取每个分组特定变量独特值的个数(number of distinct values in each group in dataframe) 目录

  2. Spark报错:The pivot column feature has more than 10000 distinct values

    (作者:陈玓玏 data-master) 用pyspark做窄表转宽表的时候,出现报错: pyspark.sql.utils.AnalysisException: u'The pivot column ...

  3. 牛客网多校第4场 D Another Distinct Values 【构造】

    题目:戳这里 题意,n*n的矩阵,只能填-1,0,1,问能不能使该矩阵的任意行和列的和都不想等. 解题思路:戳这里 可以说是一目了然了 附ac代码: 1 #include<iostream> ...

  4. hdu 6301 Distinct Values(贪心)题解

    题意:长为n的串,给你m个区间,这些区间内元素不重复,问这样的串字典序最小为? 思路:用set保存当前能插入的元素,这样就能直接插入最小元素了.对操作按l排序,因为排过的不用排,所以两个指针L,R是一 ...

  5. hdu-多校联赛6301 Distinct Values

    题目链接 Problem Description Chiaki has an array of n positive integers. You are told some facts about t ...

  6. HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)...

    HDU6301.Distinct Values 这个题就是给你区间要求区间内的数都不相同,然后要求是字典序最小,直接贪心走一遍,但是自己写的时候,思路没有错,初始化写挫了... 将区间按左端点小的排序 ...

  7. mysql忽略列,MySQL:使用DISTINCT时忽略选定的列

    Let's say: I want to query colA, colB and colC in my table. I want to see DISTINCT values but I don' ...

  8. django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

    2019独角兽企业重金招聘Python工程师标准>>> 1.多表连接查询:当我知道这点的时候顿时觉得django太NX了.   class A(models.Model):      ...

  9. 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)

    [优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...

最新文章

  1. 链表问题13——删除无序单链表中值重复出现的节点(方法二)
  2. MIS开发中C/S模式与B/S模式的结合策略
  3. OpenCV中图像显示窗口函数namedWindow()详解
  4. 阅读鸟哥的风雪之隅PHP博客的笔记(1)
  5. ThinkPHP5有关模型hasOne、hasMany、belongsTo详解
  6. 时间序列与R语言应用(part3)--趋势平稳与差分平稳
  7. P1829 [国家集训队]Crash的数字表格 / JZPTAB
  8. 度量术语之二:应用类和开发类生产率(实际度量案例)
  9. 持续集成持续部署持续交付_自动持续开发和交付混合移动应用程序
  10. [leetcode]1.Two Sum
  11. UDP协议和socketserver以及文件上传
  12. 欧拉定理以及欧拉降幂
  13. Java开发需要的官方文档
  14. 【模电】0006 有源滤波器1(一阶有源滤波器)
  15. 方差分析原理及实例应用
  16. Thinkpad E550/E450/T450/T450S黑苹果制作
  17. 李开复:搞无人车在电车难题上论争个没完,这样子不行的
  18. Android RecyclerView设置paddingBottom,scrollbars跟随滑动到底部
  19. studio3t连接mysql_Studio 3T 2019 强大的MongoDB数据库连接工具
  20. 一些你不知道的囧知识,保证让你崩溃

热门文章

  1. Android WebView重定向问题,Android Webview重定向问题解决方法
  2. 输出四位完全平方数_leetcode279_go_完全平方数
  3. python三本经典书籍-《python编程入门经典》python之父推荐这三本书让你更快入门...
  4. python自动化办公-简直出神入化,教你用Python控制Excel实现自动化办公
  5. python函数定义及调用-浅谈Python中函数的定义及其调用方法
  6. python在线编辑器手机-QPython,一个在手机上运行Python的神器
  7. 为何python不好找工作-为何python不好找工作,seo行业不好转行了
  8. 向 iOS 设备发送推送通知
  9. ble开发 linux_嵌入式开发的必备知识点
  10. vue-seamless-scroll