题解 | CVZOI Round 2 G&H题 题解

· · 个人记录

CVZOI Round 2

G题 Dabi-攒3

点击跳转

#include<bits/stdc++.h>
using namespace std;
int a[100000];
int main(){
    int n;
    bool f=0;
    cin>>n;
    if(n==1) {
        cout<<1;
        return 0;
    }
    if(n==0){
        cout<<0;
        return 0;
    }
    a[1]=1;
    if(n%3==0){
        //cout<<pow(3,n/3);
        for(int i=1;i<=n/3;i++){
            for(int j=1;j<=i;j++) a[j]*=3;
            for(int j=1;j<=i;j++) {
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        }
        for(int i=99990;i>=1;i--){
            if(a[i]==0&&!f) continue;
            else{
                f=1;
                cout<<a[i];
            } 
        }
    }
    else if(n%3==1){
        //cout<<pow(3,n/3-1)*4;
        for(int i=1;i<=n/3-1;i++){
            for(int j=1;j<=i;j++) a[j]*=3;
            for(int j=1;j<=i;j++) {
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        }
        for(int j=1;j<=99990;j++) a[j]*=4;
            for(int j=1;j<=99990;j++){
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        for(int i=99990;i>=1;i--){
            if(a[i]==0&&!f) continue;
            else{
                f=1;
                cout<<a[i];
            } 
        }
    }
    else {
        //cout<<pow(3,n/3)*2;
        for(int i=1;i<=n/3;i++){
            for(int j=1;j<=i;j++) a[j]*=3;
            for(int j=1;j<=i;j++) {
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        }
        for(int j=1;j<=99990;j++) a[j]*=2;
            for(int j=1;j<=99990;j++) {
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        for(int i=99990;i>=1;i--){
            if(a[i]==0&&!f) continue;
            else{
                f=1;
                cout<<a[i];
            } 
        }
    }
    return 0;
}

H题 镜像生长

点击跳转

这道题太有意思了,出题人好厉害

#include <iostream>
#include <cstring>
using namespace std;

// b: 基础3*3图案, a: 最终图案
char b[3][4], a[730][730];
int n, sz; // sz = 3^(n+1)

// 递归生成函数
// (x,y): 当前区块左上角坐标, s: 当前区块边长
// l: 剩余递归层数, r: 当前区块的整体旋转状态
void MirrorGrowthcnt(int x, int y, int s, int l, int r) {
    // 递归基: 到达0级图案
    if (l == 0) {
        // 将基础图案按旋转状态r填充到a中
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {
                int ni = i, nj = j; // 在基础图案中的坐标

                // 根据旋转状态计算实际坐标
                if (r == 1) { // 顺时针90度
                    ni = j;
                    nj = 2 - i;
                }
                else if (r == 2) { // 180度
                    ni = 2 - i;
                    nj = 2 - j;
                }
                else if (r == 3) { // 逆时针90度
                    ni = 2 - j;
                    nj = i;
                }
                // r==0: 不旋转

                a[x + ni][y + nj] = b[i][j];
            }
        return;
    }

    // 递归: 将当前区块分成3x3=9个小区域
    int p = s / 3; // 小区域边长 = 3^(n-1)

    // 中央区域: 直接递归, 旋转状态不变
    MirrorGrowthcnt(x + p, y + p, p, l - 1, r);

    // 上方区域: 递归(k-1)级, 旋转状态不变
    MirrorGrowthcnt(x, y + p, p, l - 1, r);

    // 下方区域: 旋转180度 (r+2)
    MirrorGrowthcnt(x + 2 * p, y + p, p, l - 1, (r + 2) % 4);

    // 左方区域: 逆时针90度 (r+3)
    MirrorGrowthcnt(x + p, y, p, l - 1, (r + 3) % 4);

    // 右方区域: 顺时针90度 (r+1)
    MirrorGrowthcnt(x + p, y + 2 * p, p, l - 1, (r + 1) % 4);

    // 四个角区域保持空白
}

int main() {
    // 输入
    cin >> n;
    for (int i = 0; i < 3; i++) cin >> b[i];

    // 计算最终图案尺寸: 3^(n+1)
    sz = 1;
    for (int i = 0; i <= n; i++) sz *= 3;

    // 初始化全为空格
    for (int i = 0; i < sz; i++)
        for (int j = 0; j < sz; j++)
            a[i][j] = ' ';

    // 递归生成图案
    MirrorGrowthcnt(0, 0, sz, n, 0);

    // 输出
    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < sz; j++)
            cout << a[i][j];
        cout << endl;
    }
    return 0;
}