首页 > 其他分享 > CF1371D

CF1371D

时间:2022-12-20 21:46:17浏览次数:37  
标签:CF1371D int value grid test include define

A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!

You are given integers n,k. Construct a grid A with size n×n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k.

Let’s define:

Ai,j as the integer in the i-th row and the j-th column.
Ri=Ai,1+Ai,2+…+Ai,n (for all 1≤i≤n).
Cj=A1,j+A2,j+…+An,j (for all 1≤j≤n).
In other words, Ri are row sums and Cj are column sums of the grid A.
For the grid A let’s define the value f(A)=(max®−min®)2+(max©−min©)2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X).
Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any.

输入格式:
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Next t lines contain descriptions of test cases.

For each test case the only line contains two integers n, k (1≤n≤300,0≤k≤n2).

It is guaranteed that the sum of n2 for all test cases does not exceed 105.

输出格式:
For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied.

After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to Ai,j.

If there are multiple answers you can print any.

输入#1

4
2 2
3 8
1 0
4 16

输出 #1

0
10
01
2
111
111
101
0
0
0
1111
1111
1111
1111

题意:

构造01矩阵使公式的值最小

分析:

按对角线构造能同时使行最大最小与列最大最小的差最小

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <climits>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mst(x, y) memset(x, y, sizeof x)
#define X first
#define Y second
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 200010, INF = 0x3f3f3f3f3f3f3f3f, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n, k;
int g[500][500];
int sumr[N], sumc[N];
void solve()
{
    mst(g, 0), mst(sumr, 0), mst(sumc, 0);
    cin >> n >> k;
    int t = k / n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j < t + i; j++)
        {
            int t = j;
            if (t > n)
                t %= n;
            g[i][t] = 1;
        }
    }

    for (int i = 1; i <= k % n; i++)
    {
        int j = t + i;
        if(j > n)
            j %= n;
        g[i][j] = 1;
    }

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            sumr[i] += g[i][j], sumc[j] += g[i][j];

    sort(sumr + 1, sumr + 1 + n), sort(sumc + 1, sumc + 1 + n);
cout << ((sumr[n] - sumr[1]) * (sumr[n] - sumr[1]) + (sumc[n] - sumc[1]) * (sumc[n] - sumc[1])) << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) cout << g[i][j]; cout << endl; } } signed main() { FAST; cin >> T; while (T--) solve(); return 0; }

 

标签:CF1371D,int,value,grid,test,include,define
From: https://www.cnblogs.com/Aidan347/p/16995136.html

相关文章