跳到正文

题解

P12549 [UOI 2025] Gift for Anton题解

Central_GYM约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月2日
篇幅
约 9 分钟
题目
P12549

版本

  1. v2当前
  2. v1

原题链接

在模拟赛里打到这个题然后吃到了场上最优解,写篇题解庆祝一下。

题意

要求你用 0,1,2,3,4 去填满 n×mn\times m 的表格,满足每个数字 xx 周围有 xx 个和它相同的数字。

分析

因为题目中的条件,我们知道如果当前的数字为 4,则它周围四个点都为 4 时才可以满足条件,因此 4 就会无限延伸下去,直到碰到边框,而边框上最多只有三个点,所以 4 是不可能填进去的。同理的 3 在网格中也会无限延伸直至碰到四角条件不成立,3 也是不可能出现在里面的。

问题简化了,因为只有 0,1,2 是可以填到网格里的,我们从小样例开始入手。

当 n=1n=1 时,2 是不能使用的(和上一段 3,4 不能使用的原因同理),我们很容易构造出下面的表格:

011011……

当 n=2n=2 时,2 我们能也只能按照 2×22\times 2 的方式填入表格:

22
22

对于每组的 2,我们需要在中间加入东西来间隔,我们考虑加入两个 1,往复就构造出了下面的表格:

122122……
122122……

这个时候我们发现上文构造的两种表格 合到一起时依旧是成立的,我们还可以把它拆分成 3×33\times 3 的小单元:

011
122
122

把他们排列起来只能解决 n,mn,m 都为 3 的倍数时的求解,但是我们根据他们取模 3 的余数来旋转每个小单元,然后单独地加上行列,于是我们就构造出了答案(为方便理解,下面是 n=4,m=5n=4,m=5 时的表格变化)。

011
122
122
110
221
221
11011
22122
22122
11011
22122
22122
11011

代码

C++
#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    //写得有些长,因为分了9种情况,但是每一个并不难
    cin>>n>>m;
    if(n%3==0&&m%3==0){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"0 1 1 ";
			}
			cout<<'\n';
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<'\n';
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<'\n';
		}
	}
    if(n%3==1&&m%3==0){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"0 1 1 ";
			}
			cout<<'\n';
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<'\n';
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<'\n';
		}
		for(int j=1;j<=m/3;j++){
    		cout<<"0 1 1 ";
		}
		cout<<'\n';
	}
	if(n%3==0&&m%3==1){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"0 1 1 ";
			}
			cout<<"0\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<"1\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<"1\n";
		}
	}
	if(n%3==1&&m%3==1){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"0 1 1 ";
			}
			cout<<"0\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<"1\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 2 2 ";
			}
			cout<<"1\n";
		}
		for(int j=1;j<=m/3;j++){
    		cout<<"0 1 1 ";
		}
		cout<<"0\n";
	}
    if(n%3==0&&m%3==2){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 1 0 ";
			}
			cout<<"1 1\n";
		}
	}
	if(n%3==2&&m%3==0){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 1 0 ";
			}
			cout<<"\n";
		}
		for(int j=1;j<=m/3;j++){
    		cout<<"2 2 1 ";
		}
		cout<<"\n";
		for(int j=1;j<=m/3;j++){
    		cout<<"2 2 1 ";
		}
		cout<<"\n";
	}
	if(n%3==2&&m%3==2){
    	for(int i=1;i<=n/3;i++){
    		for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
			for(int j=1;j<=m/3;j++){
    			cout<<"1 1 0 ";
			}
			cout<<"1 1\n";
		}
		for(int j=1;j<=m/3;j++){
    		cout<<"2 2 1 ";
		}
		cout<<"2 2\n";
		for(int j=1;j<=m/3;j++){
    		cout<<"2 2 1 ";
		}
		cout<<"2 2\n";
	}
	if(n%3==1&&m%3==2){
		for(int i=1;i<=n/3;i++){
			for(int j=1;j<=m/3;j++){
				cout<<"1 1 0 ";
			}
			cout<<"1 1\n";
			for(int j=1;j<=m/3;j++){
				cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
			for(int j=1;j<=m/3;j++){
				cout<<"2 2 1 ";
			}
			cout<<"2 2\n";
		}
		for(int j=1;j<=m/3;j++){
			cout<<"1 1 0 ";
		}
		cout<<"1 1\n";
	}
	if(n%3==2&&m%3==1){
		for(int i=1;i<=n/3;i++){
			for(int j=1;j<=m/3;j++){
				cout<<"1 2 2 ";
			}
			cout<<"1\n";
			for(int j=1;j<=m/3;j++){
				cout<<"1 2 2 ";
			}
			cout<<"1\n";
			for(int j=1;j<=m/3;j++){
				cout<<"0 1 1 ";
			}
			cout<<"0\n";
		}
		for(int j=1;j<=m/3;j++){
			cout<<"1 2 2 ";
		}
		cout<<"1\n";
		for(int j=1;j<=m/3;j++){
			cout<<"1 2 2 ";
		}
		cout<<"1\n";
	}
    return 0;
}

AC记录

原文