Codeforces Round #644 (Div. 3) G. A/B Matrix(规律构造)

题目链接


1.首先不难发现,对于矩阵而言,每一格的位置都是特定的,即处于某一行,也处在某一列。那么如果能构成n行a个1,m列b个1的矩阵,首先肯定满足:

n ∗ a = = m ∗ b n*a==m*b na==mb

2.那么我们如何构造呢,显然只需要考虑1的位置即可。那么我们先考虑每行放a个1,那么如何保证每列都有b个1?其实只需要得到之前放了多少个1,在这个基础上对该行放1时,只需用之前的总个数对m取模,就可以形成如下的一个对称的效果:

111100 111100 111100
110011 110011 110011
001111 001111 001111

而且不难发现是右→下→左→下→右这样的顺序构造的

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;

int g[105][105];
int sum[105];

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t,n,m,a,b;
    cin>>t;
    while(t--){
        cin>>n>>m>>a>>b;
        if(n*a!=m*b){
            cout<<"NO\n";
        }else{
            cout<<"YES\n";
            int s=0;
            memset(g,0,sizeof g);
            for(int i=0;i<n;i++){
                for(int j=s;j<s+a;j++)
                    g[i][j%m]=1;
                s+=a;
            }

            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++)
                    cout<<g[i][j];
                cout<<"\n";
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值