日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

A/B Matrix CodeForces - 1360G(思维构造)

發布時間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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結束的位置,下一個位置從這個位置后一位開始就可以了(代碼寫的比較丑,一開始少情況,后來補充的)
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=51; string s[maxx]; int vis[maxx][maxx]; int n,m,a,b;int main() {int t;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&n,&m,&a,&b);if(n*a!=m*b) cout<<"NO"<<endl;else{cout<<"YES"<<endl;int l=0,r=a;int cnt=0;int flag=1;for(int i=1;i<=n;i++){if(cnt==b) l+=a,r+=a,cnt=0;s[i]="";for(int j=0;j<l;j++) s[i]+='0';for(int j=l;j<r;j++) s[i]+='1';for(int j=r;j<m;j++) s[i]+='0';if(s[i].size()>m) {flag=0;break;}cnt++;}if(flag) for(int i=1;i<=n;i++) cout<<s[i]<<endl;else{memset(vis,0,sizeof(vis));int start=0;for(int i=1;i<=n;i++){for(int j=0;j<m;j++) if(j<a) vis[i][(start+j)%m]=1;start=(start+a)%m;}for(int i=1;i<=n;i++){for(int j=0;j<m;j++) cout<<vis[i][j];cout<<endl;}}}}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的A/B Matrix CodeForces - 1360G(思维构造)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。