跳到正文

题解

P5940:初探 Zeckendorf 表示

Z1qqurat约 7 分钟
原文

这是 2025年11月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2024年3月30日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 7 分钟
题目
P5940

版本

  1. v2当前
  2. v1

tag: 模拟,Zeckendorf 表示。

solve time: 4h.

一开始没有注意到输入的位置可能有重复的,拍了好久才拍出来,被坑飞了/ll。

发现这题没有 SPJ 的时候就觉得很奇怪,这真的保证是唯一解吗?手玩感觉一直把 ≥2\ge 2 的数往右边推会操作次数比较少。不过题目操作次数没有要求,试着直接观察目标局面的形式以及操作的特点。

设格子 ii 内的棋子数为 wiw_i,那么要么可以 wi←wi−1,wi−1←wi−1+1,wi−2←wi−2+1w_i\gets w_i-1,w_{i-1}\gets w_{i-1}+1,w_{i-2}\gets w_{i-2}+1,要么可以 wi←wi+1,wi−1←wi−1−1,wi−2←wi−2−1w_i\gets w_i+1,w_{i-1}\gets w_{i-1}-1,w_{i-2}\gets w_{i-2}-1,考虑每次操作后的不变量。如果能给每个位置赋一个权值 ff,使得操作前后 ∑fiwi\sum f_i w_i 不变,那么就转化为一个类似于整数拆分的问题。容易想到令 ff 为 Fibonacci 数列,最终局面相当于求一个数 s=∑fiwis=\sum f_i w_i 的 Zeckendorf 表示。

Zeckendorf’s Theorem: 一个正整数 xx 一定可以被唯一表示为若干个不相邻的 Fibonacci 数之和。

证明是平凡的。考虑归纳构造,找出 fn≤s<fn+1f_n\le s< f_{n+1},那么取出 fnf_n 作为 ss 的 Zeckendorf 表示的一项,s′=s−fn<fn+1−fn<fn−1s'=s-f_n<f_{n+1}-f_n<f_{n-1},而由归纳有 s′s' 一定可以表示出来,并且 s′<fn−1s'<f_{n-1} 所以一定存在一组表示并且没有相邻项,同时也顺带证明了 Zeckendorf 表示的唯一性。

直接求 Zeckendorf 表示。但是位置 ii 可以是负数,所以需要把 Fibonacci 数列偏移若干位之后才是 ff。ff 必须要用高精度存储(涉及高精度加法、减法、比大小以及高精度乘 int),位数达到了 21002100 左右,所以肯定不能把每个位置的 ff 都存在数组里面,具体就在循环的时候维护最近的两项 Fibonacci 数。注意一些剪枝优化。

C++
#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;
}