1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<bits/stdc++.h> using namespace std; long long fibonacci[100] = {0, 1, 1}, slen[100] = {0, 1, 3}, sum[100] = {0, 1, 2}, count_one[100] = {0, 1, 1}, a[100]; long long n, m, ans; inline void ModifyLength(register long long x, register long long &length) { memset(a, 0, sizeof(a)); length = 0; for (register long long i(m); i >= 1; --i) { if (x >= fibonacci[i + 1]) { a[i] = 1; x -= fibonacci[i + 1]; if (!length) length = i; } } } inline long long Calculate(const long long &length) { register long long num(0), t(0); for (register long long i(1); i <= length - 1; ++i) { num += a[i]; if (a[i] == 1) t = i; } if (!num) { ans += sum[length - 1] + 1; return fibonacci[length + 1]; } else { register long long x(Calculate(t)); ans += sum[length - 1] + 1 + x; return fibonacci[length + 1] + x; } } int main(int argc, char **argv) { scanf("%lld", &n); if (!n) { puts("0"); return 0; } for (register long long i(3); fibonacci[i - 1] < n; ++i) { fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; slen[i] = slen[i-1] + fibonacci[i] * i; count_one[i] = fibonacci[i] + sum[i - 2]; sum[i] = sum[i - 1] + count_one[i]; m = i - 1; } register long long t, x, y, length; for (t = 0; slen[t] <= n; ++t) continue; x = n - (slen[t - 1]); y = x % t; x = x / t + fibonacci[t + 1] - 1; ModifyLength(x, length); ans = 0; x = Calculate(length); ModifyLength(x + 1, length); for (register long long i(length); i >= length - y + 1; --i) ans += a[i]; printf("%lld\n", !n ? 0 : ans); return 0; }
|