题解 CF1637G【Birthday】
在博客园食用更佳:https://www.cnblogs.com/PinkRabbit/p/CF1637.html。
【题意简述】
你拥有
对于每次操作,你可以选择你拥有的两个数
你需要在
如果可以实现目标,你需要构造方案,否则报告之。
【题解】
首先写个暴力跑一下
这个结论的证明也很简单,考虑任意一个奇素数
首先,如果
首先一个很自然的想法是尽量将
-
$\{ 1, 2, 3, {\color{red} {2, 4, 6, 8}}, 8, {\color{blue} {16, 16, 16, 16}} \}$。
于是,已得到的
-
-
- 一个落单的
\frac{x}{2} 。
其中第 2 部分可以看作
当
在递归后,需要将已得到的整数批量操作到
我们对于
于是我们将可能落单的
代码实现中直接将递归过程定义为生成删去最后一步操作的操作序列,方便讨论。
时间复杂度为
【AC 代码】
#include <cstdio>
#include <algorithm>
#include <vector>
#include <array>
#include <bit>
#define F(i, a, b) for(int i = a; i <= (b); ++i)
void Solve();
int main() {
int tests = 1;
scanf("%d", &tests);
while (tests--)
Solve();
return 0;
}
std::vector<std::array<int, 2>> ans;
inline void add(int x, int y, int k) {
ans.push_back({x * k, y * k});
}
inline void expp(int n, int x, int k) {
while (n != x)
add(0, n, k),
add(n, n, k),
n *= 2;
}
std::vector<std::array<int, 2>> pans[8] = {
{},
{},
{},
{{1, 3}, {2, 2}},
{{1, 3}, {2, 2}},
{{1, 2}, {1, 3}, {2, 4}, {2, 6}, {4, 4}, {3, 5}, {0, 2}, {2, 2}, {0, 4}, {4, 4}},
{{1, 2}, {1, 3}, {2, 4}, {2, 6}, {4, 4}, {3, 5}, {2, 6}, {0, 4}, {4, 4}},
{{1, 2}, {3, 5}, {2, 6}, {4, 4}, {1, 0}, {1, 3}, {1, 7}, {2, 6}, {4, 4}}
};
void Solve(int n, int x, int k) {
if (n == x) {
Solve(n - 1, x, k);
return ;
}
if (n <= x / 2) {
Solve(n, x / 2, k);
F(i, 1, n - 1)
expp(x / 2, x, k);
return ;
}
if (n <= 7) {
for (auto [a, b] : pans[n])
add(a, b, k);
return ;
}
F(i, x / 2 + 1, n)
add(x - i, i, k);
// [1 ~ x-n-1], x/2, k
// [1 ~ n-x/2], x/2, 2k
int n1 = x - n - 1;
int n2 = n - x / 2;
if (n1 >= 3) {
Solve(n1, x / 2, k);
F(i, 1, n1 - 1)
expp(x / 2, x, k);
}
if (n2 >= 3) Solve(n2, x / 2, 2 * k);
expp(x / 2, x, k);
if (n1 <= 2)
F(i, 1, n1)
expp(i, x, k);
if (n2 <= 2)
F(i, 1, n2)
expp(i, x / 2, 2 * k);
if (n1 >= 3 && n2 >= 3)
add(0, x, k);
}
int n;
void Solve() {
scanf("%d", &n);
if (n == 2)
return puts("-1"), void();
int x = std::bit_ceil((unsigned)n);
if (n <= 7) {
ans = pans[n];
} else {
ans.clear();
Solve(n, x, 1);
}
add(0, x, 1);
printf("%d\n", (int)ans.size());
for (auto [a, b] : ans)
printf("%d %d\n", a, b);
}