Description

给定四个正整数 \(n,m,a,b\),构造满足条件的 \(n \times m\) 矩阵:矩阵每行包含 \(a\)\(1\),矩阵每列包含 \(b\)\(1\),矩阵剩下的位置全为 \(0\)

Solution

当且仅当 \(na=mb\) 时,可以构造出符合要求的矩阵。构造时我们只需要按行列分别循环即可。

#include <bits/stdc++.h>
using namespace std;

#define int long long 
const int N = 1000005;

void solve()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;

    vector<vector<int>> x(n+2,vector<int>(m+2));

    if(n*a==m*b)
    {
        cout<<"YES"<<endl;
        int pos=1;
        for(int i=1;i<=n;i++) 
        {
            for(int j=1;j<=a;j++)
            {
                x[i][pos++]=1;
                if(pos==m+1) pos=1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cout<<x[i][j];
            }
            cout<<endl;
        }
    }    
    else 
    {
        cout<<"NO"<<endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);

    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}

相关文章:

  • 2022-12-23
  • 2021-07-10
  • 2022-01-22
  • 2022-12-23
  • 2021-10-27
  • 2022-02-03
  • 2021-12-11
  • 2021-04-30
猜你喜欢
  • 2021-06-11
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-11-10
相关资源
相似解决方案