题目传送
题意:
给你k个1,让你构造出一个n*n的二维数组,使得数组中列和的最大值-列和的最小值的平方 + 行和的最大值 - 行和的最小值的平方最小。
思路:
要如何构造呢?
肯定是每次把1添加到一列中和一行中1最少的位置。
而我们怎么判断最小值是多少呢?
我们想想其实只有0和2中俩种,因为按这种摆法最多也就是 最大值和最小值的差值为1。
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >> t;
while(t--)
{
int n,k;
cin >> n >> k;
if(k % n == 0) cout << 0 << endl;
else cout << 2 << endl;
int arr[n+5][n+5] = {0};
for(int i = 1;i <= n;i++)
{
ll m = i;
for(int j = 1;j <= n;j++)
{
if(m > n) m = 1;
if(k <= 0) break;
arr[j][m] = 1,m++,k--;
}
if(k <= 0) break;
}
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
j != n ? cout << arr[i][j] : cout << arr[i][j] << endl;
}
}