EOJ Monthly 2018.10

#3642 oxx 的小姐姐们

oxx 和他的小姐姐(们)躺在图书馆前的大草坪上看星星。

有强迫症的 oxx 想要使得他的小姐姐们正好躺成一块 n×m 的长方形。

已知小姐姐的形状是 1×p 的长方形(可以横着或竖着躺)。小姐姐从 1 到 nm 编号总共有 nm 个(如果可以的话,绝对够用)。

P.S. 小姐姐是 1×p 的是因为她们比较苗条。

输入

输入三个整数 nmp (1n,m,p100p 是质数)。

输出

如果不行,输出 No

否则输出 Yes。随后输出 n 行 m 列正整数用空格隔开。同一个小姐姐用相同的数字表示,不同的小姐姐用不同的数字表示。数字应是在 [1nm] 范围内的正整数。同一个数字至多出现 p 次,这 p 次应该在横向连续,或者纵向连续。

如果有多解输出任意一解。

样例

input
2 3 2
output
Yes
2 2 3
1 1 3
input
3 3 2
output
No
input
3 3 3
output
Yes
2 2 2
1 1 1
3 3 3
input
2 3 2
output
Yes
6 3 3
6 4 4
input
4 2 2
output
Yes
2 7
2 7
5 5
3 3

提示

请注意对于最后一组样例输出:

2 1
2 1
1 2
1 2

是不合法的。因为不同的小姐姐必须用不同的数字表示。你居然把 1 号小姐姐和 2 号小姐姐克隆了 QAQ。

解题思路:p是质数,如果n*m%p!=0时,显然为"No";否则一定能用多块1*p的矩形覆盖满方阵:考虑先对行进行横线填充,再对行剩下的列进行纵向填充。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=105;
 5 int n,m,p,t,cnt,tp,mp[maxn][maxn];
 6 int main(){
 7     while(cin>>n>>m>>p){
 8         t=n*m;
 9         if(t%p)cout<<"No"<<endl;
10         else{
11             memset(mp,0,sizeof(mp));cnt=1;
12             cout<<"Yes"<<endl;
13             for(int x=1;x<=n;++x){///先对行进行填充
14                 for(int y=1;y<=m-m%p;++y){
15                     mp[x][y]=cnt;
16                     if(y%p==0)cnt++;///填满一块1*p的矩形
17                 }
18             }
19             for(int y=m-m%p+1;y<=m;++y){///再对剩余的列进行填充
20                 for(int x=1;x<=n-n%p;++x){
21                     mp[x][y]=cnt;
22                     if(x%p==0)cnt++;
23                 }
24             }
25             for(int i=1;i<=n;++i)
26                 for(int j=1;j<=m;++j)
27                     cout<<mp[i][j]<<(j==m?'\n':' ');
28         }
29     }
30     return 0;
31 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9967031.html