Slagalica 题解
2023-04-23:先吐槽一两句,洛谷这道题的题面是有问题的,原英文题面是要求为恰好第一次恢复原样,但翻译的题面没有体现出第一次这一限制条件 QAQ。
2024.1.11 update: 忽然发现题面改了,记录一下,留恋qwq,还补了个可能不是很详细的构造证明嘿嘿 (º﹃º )。
简化题意
已知有如图所示
操作
操作
请构造一组操作次数小于等于
题目分析
如果我们将每一次执行操作序列后的位置与原位置连边,那么这个变换就能以总点数为
考虑到总点数和操作序列的次数的限制,我们应该使得总环长尽可能的小,那么我们就可以对 (感觉不如感性理解)
有一个显然的结论,一个序列
接下来我们要考虑如何去一个一个把环给构造出来。
通过瞎搞模拟我们能够发现,进行
我们若是想交换
于是我们可以总结一下:对于任意相邻位置,我们可以将其放在一个满足操作
既然有了交换任意两个相邻点的方案,这题便迎刃而解惹,我们直接在一条相邻点连成的、能覆盖整张图的链上(比如蛇形填数、
具体细节详见代码:
#include <bits/stdc++.h>
#define inLL long long
using namespace std;
int n, m, x, y, az, tot;
inLL k;
struct out {
char a;
int x, y;
}ans[500005];
template <typename T> void read(T& x) {
x = 0; int f = 0; char c = getchar();
while(c < '0' || c > '9') f |= (c == '-'), c=getchar();
while(c >= '0' && c <= '9') x=(x<<1)+(x<<3)+(c^48), c=getchar();
x=(f ? -x : x);
}
int lne; char put[105];
template <typename T> void write(T x, char ch) {
lne = 0; if(x < 0) putchar('-'), x=-x;
do { put[++lne]=x%10, x/=10; } while(x);
while(lne) putchar(put[lne--]^48);
putchar(ch);
}
void one(int xx, int yy) {//一次单位操作
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'T', xx, yy};
ans[++tot]=out{'T', xx, yy};
}
void up(int xx, int yy) {//交换平行四边形的上边两点
one(xx, yy);
}
void down(int xx, int yy) {//交换平行四边形的底边两点
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
one(xx, yy);
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
}
void left(int xx, int yy) {//交换平行四边形的左边的两点
ans[++tot]=out{'R', xx, yy};
one(xx, yy);
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
}
void right(int xx, int yy) {//交换平行四边形的右边的两点
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
ans[++tot]=out{'R', xx, yy};
one(xx, yy);
ans[++tot]=out{'R', xx, yy};
}
void nxt() {
(x&1 ? ((y^m) ? ++y : ++x) : ((y^1) ? --y : ++x));
//s型路线遍历嘻嘻,用了位运算和三目运算符优化:
//x为奇数时,若y等于m则x+1,否则y+1;
//x为偶数时,若y等于1则x+1,否则y-1;
}
void solve(inLL k/*当前需要构造的环长*/) {
if(k > az) {//剩余点数不够了,一定无解,输出-1
puts("-1");
exit(0);
}
az-=k;//计算剩余点数
for(int o = 1; o <= k-1; ++o)
(x&1 ? ((y^m) ? ((x^1) ? up(x-1, y) : down(x, y)): right(x, y-1)) :
((y^1) ? up(x-1, y-1) : left(x, y))), nxt()/*交换下一处*/;
//进行交换操作,此处依旧用了位运算和三目运算符优化:
//x为奇数时,若y等于m则取右下角为(x-1,y)的平行四边形交换右边两点,否则判断x是否等于1,
//等于1则取右下角为(x,y)的平行四边形交换下边两点,不等于1就取右下角为(x-1,y)的平行四边形交换上边两点;
//x为偶数时,若y等于1则取右下角为(x,y)的平行四边形交换左边两点,否则直接取右下角为(x-1,y-1)的平行四边形交换上边两点
nxt();//这个环构造玩了,走到下个环的起始位置
}
signed main() {
read(n), read(m), read(k);
x=y=1, az=n*m;
for(inLL i = 2; i*i <= k; ++i) {
if(k%i) continue;
inLL pp = 1;
while(!(k%i))
pp*=i, k/=i;//分解k
solve(pp);//构造环
}
if(k^1)
solve(k);
write(tot, '\n');//输出,详见题目"输出格式"部分
for(int i = 1; i <= tot; ++i)
putchar(ans[i].a), putchar(' '), write(ans[i].x, ' '), write(ans[i].y, '\n');
return 0;
}
芜湖,完结撒花 ヾ(◍°∇°◍)ノ゙。