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:

|010001100100001010|

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,m,a,b,要求输出YES和一个n*m的数组,其中每行有a个1,每列有b个1,其余为0.若没有符合条件的数组,输出NO。

贴一个我自己的wrong的代码,暂时还没完全弄明白。

#include<iostream>
#include<algorithm>
using namespace std;int t, n, m, a, b;
int main(){scanf("%d", &t);while(t--){scanf("%d%d%d%d", &n, &m, &a, &b);if(n*a != m*b){    printf("NO\n");continue;}int line[51], list[51];//行、列for(int i=0; i<n; i++)line[i] = a;for(int i=0; i<m; i++)list[i] = b;printf("YSE\n");for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(line[i] && list[j]){printf("1");line[i]--;list[j]--;}else printf("0");}printf("\n");}}
}

吸收各方大佬以后按照他们的方法写的ac的代码

#include<iostream>
using namespace std;
int s[52][52];
int t, n, m, a, b;
int main(){scanf("%d", &t);while(t--){scanf("%d%d%d%d", &n, &m, &a, &b);if(n*a != m*b){    printf("NO\n");continue;}for(int i=0; i<n; i++)for(int j=0; j<m; j++)s[i][j] = 0;for(int i=0, j=0; i<n; i++, j=(j+a)%m)for(int k=0; k<a; k++)s[i][(j+k)%m] = 1;printf("YES\n");for(int i=0; i<n; i++){for(int j=0; j<m; j++)printf("%d", s[i][j]);printf("\n");}}return 0;
}

G - A/B Matrix CodeForces - 1360G相关推荐

  1. 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 matr ...

  2. Codeforces Round #644 (Div. 3) G.A/B Matrix

    Codeforces Round #644 (Div. 3) G.A/B Matrix 题目链接 You are given four positive integers n, m, a, b (1≤ ...

  3. CodeForces - 1360G A/B Matrix(最大流)

    题目链接:点击查看 题目大意:给出一个 n * m 大小的空矩阵,要求在某些位置放置 1 ,其余位置放置 0 ,使得每行都有恰好 a 个 1 ,且每列恰好有 b 个 1 ,给出一种构造方案或者判断是否 ...

  4. 省赛训练3 G HDU 1759 Matrix Revolution(BFS)

    Matrix Revolution Time Limit: 3000 ms /Memory Limit: 32768 kb Description Lele 现在不仅会整数A+B,A*B,还会矩阵A+ ...

  5. Maximal Binary Matrix CodeForces - 803A (贪心+实现)

    题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...

  6. Codeforces Round #725 (Div. 3) G. Gift Set 二分

    传送门 文章目录 题意: 思路: 题意: 有两种物品分别有x,yx,yx,y个,每次可以从一个拿出aaa个,另一个拿出bbb个分成一组,问最多能分成多少组. 思路: 这个题有一个显然的单调性,所以二分 ...

  7. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  8. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  9. 用g.raphael.js高速绘制饼图、柱状图、点状图、折线图(上)

    首先介绍一下什么是g.raphael.这个又要说到什么是raphael.js.raphael是一个javascript库,可以用来跨浏览器绘制各种图形,只要是你想得到的图形都可以用raphael绘制出 ...

最新文章

  1. 使用 EclEmma 来显示代码覆盖率
  2. php调用搜狗ocr接口,搜狗ocr识别接口
  3. js实现旋转木马轮播图
  4. android编程获取网络和wifi状态及调用网络设置界面,Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面 - Android平台开发技术 - 博客园...
  5. python绘制直方图显示数字_python plotly绘制直方图实例详解
  6. C - Log Calculator FZU - 2036
  7. 链接克隆 完整克隆_深入克隆
  8. javaweb学习总结(四十六)——Filter(过滤器)常见应用
  9. [论文翻译] Class-incremental learning: survey and performance evaluation on image classification
  10. python基础之字符编码、文件处理
  11. 虚拟化未来是I don’t care
  12. 图像处理小tip——中值滤波的多种实现(包括快速中值滤波算法)
  13. wifi分析仪怎么看哪个信道好_怎么查看周围的WiFi网络使用了哪些信道
  14. 罗技键盘+android风格,Logitech 罗技 K480 蓝牙键盘,IOS、OSX 和安卓三大系统使用体验...
  15. mysql代码创建表博客园_数据库——用代码创建表
  16. 【obs】27:deps 构建说明及studio的vs2019构建及裁剪
  17. 【托业】【新托业TOEIC新题型真题】学习笔记8-题库五-P7
  18. 平面设计基本艺术表现形式有哪些
  19. java剑姬_Java虚拟机非常有用的性能监控工具
  20. 假设 A 类有如下定义,设 a 是 A 类的一个实例,下列语句调用哪个是错误的?()

热门文章

  1. 使用getElementsByTagName()和namedItem()获取特定元素
  2. 微信小程序下拉触底卡顿
  3. n个元素的所有子集(递归+非递归 +不去重)
  4. 当没有接口文档时候,测试人员如何测试?
  5. greedy策略求解活动选择问题 ActivitySelectProblem
  6. halcon中面到面的距离_halcon学习网
  7. Python (2)
  8. JSPlumb文档翻译
  9. zabbix结合qqmail发送故障信息
  10. 数据结构实战-死磕王争