803A. Maximal Binary Matrixcon

题目

链接:https://codeforces.com/problemset/problem/803/A

time limit per test:1 second;memory limit per test:256 megabytes

You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.

One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.

If there exists no such matrix then output -1.

Input

The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).

Output

If the answer exists then output resulting matrix. Otherwise output -1.

Examples

Input

2 1

Output

1 0 
0 0 

Input

3 2

Output

1 0 0 
0 1 0 
0 0 0 

Input

2 5

Output

-1

 

思路

 

        经过一通试验,我发现k=0 时要单独处理一下 —— 大概跟我的思路有关。什么时候不存在满足题目要求的对称矩阵?1 的数量多于矩阵中 0 的数量时,不存在这样的矩阵。然后就要考虑怎么用 1 填充矩阵。既然要让矩阵的字典序最大,矩阵左上角的元素肯定填上1 ,然后要再保持矩阵中元素关于对角线对称的情况下,恰当地填充元素,使得矩阵的字典序更大。尽可能地从上到下,从左到右填充,已经填充的格子要避开,一旦上三角的某个位置填充一个 1 ,相应地就要在下三角的对应位置填充一个 1 ,注意到,如果填充的格子数已经到了k-1 ,就不能再往对角线以外的地方填充了,否则得到的就不是对称的矩阵了。

代码

 

n,k=map(int,input().split())
matrix=[[0 for j in range(n)] for i in range(n)]
# print(matrix)
if (k%2==0 and k>n*n) or (k>n*n):
    print(-1)
else:
    if k!=0:    # 处理k不等于0的情况,等于0就不用处理了
        c=0
        q=-1
        d=-1    # 标记对角线上有了几个1
        flag=True
        for i in range(n):
            q+=1
            for j in range(q,n):
                matrix[i][j]=1
                matrix[j][i]=1
                if i!=j:
                    c+=2
                    if k-c==1:
                        matrix[d+1][d+1]=1
                        c+=1
                else:
                    d+=1
                    c+=1
                    if k-c==1:
                        matrix[i+1][j+1]=1
                        c+=1
                if c==k:
                    flag=False
                    break
            if flag==False:
                break
    for v in range(n):
        for w in range(n):
            print(matrix[v][w],end=" ")
        print("")

 

你可能感兴趣的:(python,constructive,算法)