BZOJ2173: 整数的lqp拆分
Sol
构造 \(fib\) 数列的母函数 \(F(x)\)
那么答案就是
\[[x^n]\sum_{i=1}^{\infty}F^i(x)=[x^n]\frac{F(x)}{1-F(x)}
\]
而
\[F(x)=xF(x)+x^2F(x)+x,F(x)=\frac{x}{1-x-x^2}
\]
所以答案就是
\[[x^n]\frac{x}{1-2x-x^2}
\]
递推式出来了直接 \(\Theta(n)\) 递推
或者求出通项
\[\frac{(\sqrt{2}+1)^n-(1-\sqrt{2})^n}{2\sqrt{2}}
\]
然后 \(\Theta(logn)\) 出解
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod(1e9 + 7);
inline int Inc(int x, int y) {
return x + y >= mod ? x + y - mod : x + y;
}
inline int Dec(int x, int y) {
return x - y < 0 ? x - y + mod : x - y;
}
inline int Pow(ll x, int y) {
register ll ret = 1;
for (x = (x % mod + mod) % mod; y; y >>= 1, x = x * x % mod)
if (y & 1) ret = ret * x % mod;
return ret;
}
struct Pair {
int a, b;
inline Pair() {
a = b = 0;
}
inline Pair(int _a, int _b) {
a = _a, b = _b;
}
inline Pair operator +(Pair x) const {
return Pair(Inc(a, x.a), Inc(b, x.a));
}
inline Pair operator -(Pair x) const {
return Pair(Dec(a, x.a), Dec(b, x.b));
}
inline Pair operator *(Pair x) const {
return Pair(((ll)a * x.a + (ll)2 * b * x.b) % mod, ((ll)a * x.b + (ll)x.a * b) % mod);
}
inline Pair operator /(Pair x) const {
register Pair t1(a, b), t2(x.a, mod - x.b), t3(Pow((ll)x.a * x.a - (ll)2 * x.b * x.b, mod - 2), 0);
return t1 * t2 * t3;
}
} v1, v2, v3;
int n, x;
int main() {
scanf("%d", &n), v1 = Pair(1, 1), v2 = Pair(1, mod - 1);
for (v3 = Pair(1, 0), x = n; x; x >>= 1, v1 = v1 * v1) if (x & 1) v3 = v3 * v1;
for (v1 = Pair(1, 0), x = n; x; x >>= 1, v2 = v2 * v2) if (x & 1) v1 = v1 * v2;
v1 = (v3 - v1) / Pair(0, 2), printf("%d\n", v1.a);
return 0;
}