ARC110F Esoswap

发布时间 2023-11-27 17:00:57作者: cxqghzj

题意

给定一个序列,每次操作 \(swap(p_i, p_{(i + p_i) mod N})\)

求将她变得有序的方案。

Sol

我们考虑对于一个位置不断的操作,发现最后一定会变成 \(0\)

我们设她为 \(p_x\)

考虑操作 \(p_{x - 1}\)

发现当 \(p_{x - 1}\) 被交换变为 \(0\) 的时候 \(p_{x - 1} = 1\)

也就是交换后 \(p_{x - 1} = 0, p_x = 1\)

考虑把这个玩意扩展到 \([1, n]\)

然后发现对了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
int main() {
	int n = read();
	write(n * n), puts("");
	for (int i = n - 1; ~i; i--) {
		int tp = n;
		while (tp--) write(i), puts("");
	}

	return 0;
}