题解:P18011 [GCPC 2022] Lots of Land

· · 题解

构造水题。

若 l \times w 不能被 n 整除,则无解。

设 d=\gcd(l,n),m=\dfrac{n}{d},h=\dfrac{l}{d},b=\dfrac{w}{m}。 把大矩形分成 d 条高 h 的横条,每条再竖直切成 m 块宽 b 的小矩形,依次填字母输出。

时间复杂度:O(l \times w)。

#include<bits/stdc++.h>
using namespace std;
int l,w,n;
int main(){
    cin>>l>>w>>n;
    int mj=l*w;
    if(mj%n!=0){
        cout<<"impossible";
        return 0;
    }
    int d=__gcd(l,n),m=n/d,h=l/d,b=w/m;
    for(int i=0;i<l;i++){
        string s;
        int st=i/h;
        for(int j=0;j<w;j++){
            int c=j/b;
            int idx=st*m+c;
            s+='A'+idx;
        }
        cout<<s<<'\n';
    }
    return 0;
}