tag: 模拟,Zeckendorf 表示。
solve time: 4h.
一开始没有注意到输入的位置可能有重复的,拍了好久才拍出来,被坑飞了/ll。
发现这题没有 SPJ 的时候就觉得很奇怪,这真的保证是唯一解吗?手玩感觉一直把 的数往右边推会操作次数比较少。不过题目操作次数没有要求,试着直接观察目标局面的形式以及操作的特点。
设格子 内的棋子数为 ,那么要么可以 ,要么可以 ,考虑每次操作后的不变量。如果能给每个位置赋一个权值 ,使得操作前后 不变,那么就转化为一个类似于整数拆分的问题。容易想到令 为 Fibonacci 数列,最终局面相当于求一个数 的 Zeckendorf 表示。
Zeckendorf’s Theorem: 一个正整数 一定可以被唯一表示为若干个不相邻的 Fibonacci 数之和。
证明是平凡的。考虑归纳构造,找出 ,那么取出 作为 的 Zeckendorf 表示的一项,,而由归纳有 一定可以表示出来,并且 所以一定存在一组表示并且没有相邻项,同时也顺带证明了 Zeckendorf 表示的唯一性。
直接求 Zeckendorf 表示。但是位置 可以是负数,所以需要把 Fibonacci 数列偏移若干位之后才是 。 必须要用高精度存储(涉及高精度加法、减法、比大小以及高精度乘 int),位数达到了 左右,所以肯定不能把每个位置的 都存在数组里面,具体就在循环的时候维护最近的两项 Fibonacci 数。注意一些剪枝优化。
#include <bits/stdc++.h>
#define ALL(v) begin(v), end(v)
#define All(v, l, r) &v[l], &v[(r) + 1]
using i64 = int64_t;
using u64 = uint64_t;
using db = double;
using std::cin;
using std::cout;
constexpr int N = 2e4, D = 100;
int n;
std::array<i64, N> w;
struct Bigint {
int len;
std::array<i64, 2500> a;
Bigint() {}
Bigint(int l, i64 x) {
len = l, std::fill(ALL(a), 0);
for (int i = 0; i <= l; ++i) a[i] = x % 10, x /= 10;
}
void init(int l, i64 x) {
len = l;
for (int i = 0; i <= l; ++i) a[i] = x % 10, x /= 10;
}
bool operator<= (const Bigint y) {
if (len != y.len) return len < y.len;
for (int i = len; ~i; --i) {
if (a[i] < y.a[i]) return true;
if (a[i] > y.a[i]) return false;
}
return true;
}
Bigint operator+ (const Bigint y) {
Bigint z(std::max(len, y.len) + 5, 0);
for (int i = 0; i <= z.len; ++i) {
z.a[i] += a[i] + y.a[i];
if (z.a[i] > 9) z.a[i] -= 10, z.a[i + 1]++;
}
while (z.a[z.len] == 0) --z.len;
return z;
}
friend void operator+= (Bigint &x, const Bigint y) {
x = x + y;
}
Bigint operator- (const Bigint y) {
Bigint z(std::max(len, y.len), 0);
for (int i = 0; i <= z.len; ++i) {
z.a[i] += a[i] - y.a[i];
if (z.a[i] < 0) {
i64 k = (-z.a[i] - 1) / 10 + 1;
z.a[i] += 10 * k, z.a[i + 1] -= k;
}
}
while (z.a[z.len] == 0) --z.len;
return z;
}
friend void operator-= (Bigint &x, const Bigint y) {
x = x - y;
}
Bigint operator* (const i64 y) {
Bigint z(len + 20, 0);
for (int i = 0; i <= z.len; ++i) {
z.a[i] += a[i] * y;
i64 k = z.a[i] / 10;
z.a[i] -= k * 10, z.a[i + 1] += k;
}
while (z.a[z.len] == 0) --z.len;
return z;
}
friend void operator*= (Bigint &x, const i64 y) {
x = x * y;
}
void print() {
for (int i = len; ~i; --i) cout << a[i];
}
} s, f0, f1;
std::array<i64, N> val;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n, f0.init(0, 1), f1.init(0, 1);
int mx = 0;
for (int i = 1, x, y; i <= n; ++i)
cin >> x >> y, val[x + D] += y, mx = std::max(mx, x + D);
for (int i = 2; i <= mx; ++i) {
std::swap(f0, f1), f1 += f0;
if (val[i]) s += f1 * val[i];
}
while (f1 <= s) std::swap(f0, f1), f1 += f0, mx++;
std::vector<int> ans;
for (int i = mx; ~i; --i) {
if (!s.len && !s.a[0]) break;
if (f1 <= s) s -= f1, ans.emplace_back(i - D);
std::swap(f0, f1), f0 -= f1;
}
std::reverse(ALL(ans));
for (int i : ans) cout << i << ' ';
cout << "\n";
return 0;
}