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

题目链接:https://codeforces.ml/contest/1360/problem/G

题意:在一个n行m列的矩阵中 问是否存在每行有a个1 每列有b个1的矩阵 

思路:首先判断能否能放  如果能放的话 要构造的话就要考虑贪心的构造 列和行同时考虑的话很难做

那么考虑首先满足每一行放a个1 然后再考虑如何使得列的条件满足 

每一行放a个后 那些没放的位置就要在下一行继续放 只有这样贪心才能尽量地使每一列的1相等   (x放完了一行后 继续让x=1开始从头放)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define pb push_back
 6 const int maxn=1e6+10;
 7 const int mod=1e9+7;
 8 int ans[55][55];
 9 
10 int main()
11 {
12     ios::sync_with_stdio(false);
13     cin.tie(0);
14     int t;
15     cin>>t;
16     while(t--)
17     {
18         int n,m,a,b;
19         cin>>n>>m>>a>>b;
20         for(int i=1;i<=n;i++)
21         {
22             for(int j=1;j<=m;j++)
23             {
24                 ans[i][j]=0;
25             }
26         }
27         int sum=a*n;
28         if(sum%m==0&&sum/m==b)
29         {
30             int x=1;
31             for(int i=1;i<=n;i++)
32             {
33                 for(int j=1;j<=a;j++)
34                 {
35                     ans[i][x++]=1;
36                     if(x>m)
37                         x=1;
38                 }
39             }
40             cout<<"YES"<<'\n';
41             for(int i=1;i<=n;i++)
42             {
43                 for(int j=1;j<=m;j++)
44                 {
45                     cout<<ans[i][j];
46                 }
47                 cout<<'\n';
48             }
49         }
50         else
51         {
52             cout<<"NO"<<'\n';
53         }
54     }
55 
56 
57 
58 
59 }
View Code

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =1e5+10;
 6 const int mod=1e9+7;
 7 int s[55][55];
 8 
 9 
10 
11 int main()
12 {
13     ios::sync_with_stdio(false);
14     cin.tie(0);
15     int t;
16     cin>>t;
17     while(t--)
18     {
19         int n,m,a,b;
20         cin>>n>>m>>a>>b;
21         memset(s,0,sizeof(s));
22         int x=0;
23         for(int i=0;i<n;i++)
24         {
25             for(int j=0;j<a;j++)
26             {
27                 s[i][x++]=1;
28                 x%=m;
29             }
30         }
31         int f=0;
32         for(int j=0;j<m;j++)
33         {
34             int sum=0;
35             for(int i=0;i<n;i++)
36             {
37                 sum+=s[i][j];
38             }
39             if(sum!=b)
40                 f=1;
41         }
42         if(f)
43             cout<<"NO"<<'\n';
44         else
45         {
46             cout<<"YES"<<'\n';
47             for(int i=0;i<n;i++)
48             {
49                 for(int j=0;j<m;j++)
50                 {
51                     cout<<s[i][j];
52                 }
53                 cout<<'\n';
54             }
55         }
56     }
57 
58 
59 
60 
61 
62 }
View Code

 

posted @ 2020-06-26 13:16  canwinfor  阅读(128)  评论(0)    收藏  举报