A/B Matrix (Codeforces 1360G)
Tags: Greedy, constructive algorithms

You are given four positive integers $$$n$$$, $$$m$$$, $$$a$$$, $$$b$$$ ($$$1 \le b \le n \le 50$$$; $$$1 \le a \le m \le 50$$$). Find any such rectangular matrix of size $$$n \times m$$$ that satisfies all of the following conditions:  each row of the matrix contains exactly $$$a$$$ ones;  each column of the matrix contains exactly $$$b$$$ ones;  all other elements are zeros. If the desired matrix does not exist, indicate this.For example, for $$$n=3$$$, $$$m=6$$$, $$$a=2$$$, $$$b=1$$$, there exists a matrix satisfying the conditions above:$$$$$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$$$$

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.Each test case is described by four positive integers $$$n$$$, $$$m$$$, $$$a$$$, $$$b$$$ ($$$1 \le b \le n \le 50$$$; $$$1 \le a \le m \le 50$$$), where $$$n$$$ and $$$m$$$ are the sizes of the matrix, and $$$a$$$ and $$$b$$$ are the number of ones for rows and columns, respectively.

Output: For each test case print:  "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or  "NO" (without quotes) if it does not exist. To print the matrix $$$n \times m$$$, print $$$n$$$ rows, each of which consists of $$$m$$$ numbers $$$0$$$ or $$$1$$$ describing a row of the matrix. Numbers must be printed without spaces.


Sample Inputs:
5
3 6 2 1
2 2 2 1
2 2 2 2
4 4 2 2
2 1 1 2

Sample Outputs:
YES
010001
100100
001010
NO
YES
11
11
YES
1100
1100
0011
0011
YES
1
1

Solution (Python):
for i in range(int(input())):
    (n, m, a, b) = map(int, input().split())
    x = m - a
    (s, t) = ('', '1' * a + '0' * x)
    for i in range(n):
        s += '\n' + t
        t = t[x:] + t[:x]
    print('YES' + s if n * a == m * b else 'NO')

Solution (Python):
for i in range(int(input())):
    (n, m, a, b) = map(int, input().split())
    x = m - a
    (s, t) = ('', '1' * a + '0' * x)
    for i in range(n):
        s += '\n' + t
        t = t[x:] + t[:x]
    print('YES' + s if n * a == m * b else 'NO')

Solution (Python):
import sys
import heapq
import random
import collections
from functools import lru_cache

def solve(n, m, a, b):
    console(n, m, a, b)
    if n * a != m * b:
        console('case 1b', n / b, m / a)
        return 'NO'
    res = []
    t = '1' * a + '0' * (m - a)
    for i in range(n):
        res.append(t)
        t = t[m - a:] + t[:m - a]
    return 'YES\n' + '\n'.join(res)

def console(*args):
    return
for case_num in range(int(input())):
    (n, m, a, b) = list(map(int, input().split()))
    res = solve(n, m, a, b)
    print(res)