A/B Matrix CodeForces - 1360G(思维构造)
You are given four positive integers n, m, a, b (1≤b≤n≤50; 1≤a≤m≤50). Find any such rectangular matrix of size n×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:
∣∣∣∣010100001010001100∣∣∣∣
Input
The first line contains an integer t (1≤t≤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≤b≤n≤50; 1≤a≤m≤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×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.
Example
Input
5
3 6 2 1
2 2 2 1
2 2 2 2
4 4 2 2
2 1 1 2
Output
YES
010001
100100
001010
NO
YES
11
11
YES
1100
1100
0011
0011
YES
1
1
思路:
如果n * a!=m*b的話,就不能構造成功。
在構造成功的情況,對于當前行,我們記錄一下這一行1結束的位置,下一個位置從這個位置后一位開始就可以了(代碼寫的比較丑,一開始少情況,后來補充的)
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的A/B Matrix CodeForces - 1360G(思维构造)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #64
- 下一篇: The Best Vacation Co