G. A/B Matrix(规律构造+均分)

https://codeforces.com/problemset/problem/1360/G


思路:n*a!=m*b一定无解。

不然按照每行先给a个1,然后下一行从上一行的最后一个位置进行给1.

没有%m的时候是个均分的过程,%m后同样是每个位置上均分。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=60;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ans[maxn][maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL t;cin>>t;
   while(t--){
     memset(ans,0,sizeof(ans));
     LL n,m,a,b;cin>>n>>m>>a>>b;
     if(n*a!=m*b){
        cout<<"NO"<<"\n";continue;
     }
     cout<<"YES"<<"\n";
     LL last=1;
     for(LL i=1;i<=n;i++){
        LL cnt=0;
        while(cnt<a){
           ans[i][last]=1;
           cnt++;
           last=(last%m)+1;
        }
     }
     for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++) cout<<ans[i][j];
        cout<<"\n";
     }
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115359375