跳到正文

题解

题解:P18011 [GCPC 2022] Lots of Land

htjiajia约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年11月30日
收录
2026年11月30日
更新
2026年11月30日
篇幅
约 1 分钟
题目
P18011

版本

  1. v1当前

构造水题。

若 l×wl \times w 不能被 nn 整除,则无解。

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

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

C++
#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;
}
原文