EOJ Monthly 2018.10 A. oxx 的小姐姐们
一场比赛就弄了一个题,然后过测试的时候就过了一小半样例就卡了,结束后要了别人的代码,发现自己整麻烦了,但是还是不知道自己错在了哪里,因为本地跑的时候没发现问题。
Time limit per test: 1.0 seconds
Memory limit: 512 megabytes
oxx 和他的小姐姐(们)躺在图书馆前的大草坪上看星星。
有强迫症的 oxx 想要使得他的小姐姐们正好躺成一块 n×m 的长方形。
已知小姐姐的形状是 1×p 的长方形(可以横着或竖着躺)。小姐姐从 1 到 nm 编号总共有 nm 个(如果可以的话,绝对够用)。
P.S. 小姐姐是 1×p 的是因为她们比较苗条。
Input
输入三个整数 n, m, p (1≤n,m,p≤100,p 是质数)。
Output
如果不行,输出 No。
否则输出 Yes。随后输出 n 行 m 列正整数用空格隔开。同一个小姐姐用相同的数字表示,不同的小姐姐用不同的数字表示。数字应是在 [1,nm] 范围内的正整数。同一个数字至多出现 p 次,这 p 次应该在横向连续,或者纵向连续。
如果有多解输出任意一解。
Examples
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
Note
请注意对于最后一组样例输出:
2 1
2 1
1 2
1 2
是不合法的。因为不同的小姐姐必须用不同的数字表示。你居然把 1 号小姐姐和 2 号小姐姐克隆了 QAQ。
自己的错误代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
int main(){
int n,m,p,i,j,k,h;
scanf("%d%d%d",&n,&m,&p);
if(n%p!=0&&m%p!=0){
printf("No");
}
else{
printf("Yes\n");
if(n%p==0){
h=1;
for(i=1;i<=n/p;i++){
for(j=1;j<=p;j++){
for(k=1;k<=m;k++){
printf("%d ",k+h-1);
}
if(i!=n/p||j!=p)
printf("\n");
// printf("%d",i);
}
h=h+m;
}
}
else{
h=1;
for(i=1;i<=n;i++){
for(j=0;j<=m/p*(n-1);j=j+n){
for(k=1;k<=p;k++){
printf("%d ",j+h);
}
}
if(i!=n)
printf("\n");
h++;
}
}
}
return 0;
}
要的别人的AC代码
#include<iostream>
#include<cstdio>
using namespace std;
int mp[101][101];
int main()
{
int n,m,p;
int cnt,sum;
while(~scanf("%d%d%d",&n,&m,&p))
{
if(!(n%p))
{
cnt=1;
sum=0;
for(int j=1;j<=m;j++)
for(int i=1;i<=n;i++)
{
mp[i][j]=cnt;
sum++;
if(sum>=p)
{
sum=0;
cnt++;
}
}
printf("Yes\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
printf("%d%c",mp[i][j]," \n"[j==m]);
}
else if(!(m%p))
{
cnt=1;
sum=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
mp[i][j]=cnt;
sum++;
if(sum>=p)
{
sum=0;
cnt++;
}
}
printf("Yes\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
printf("%d%c",mp[i][j]," \n"[j==m]);
}
else
printf("No\n");
}
return 0;
}