题目描述
https://www.luogu.com.cn/problem/P4451
简要题意:令 为斐波那契数第 项,求 ,
Solution
令 为斐波那契数的生成函数,那么容易得到答案的生成函数就是
我们考虑 这个生成函数,我们知道 ,那么 ,我们尝试求出它的递推式
我们知道分母的递推式就是 ,我们将这个递推式乘上 ,那么变成 ,解特征方程能得到 ,我们知道 ,能够得到
我们知道 在 下有二次剩余 ,那么我们直接做快速幂即可,注意指数要对 取模
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
| #include <iostream> #include <cstring> #define maxn 100010 #define ll long long using namespace std;
const int p = 1000000007; const int pp = 1000000006; const int sqrt2 = 59713600;
ll pow_mod(ll x, ll n) { ll s = 1; for (; n; n >>= 1, x = x * x % p) if (n & 1) s = s * x % p; return s; }
int n; char s[maxn];
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> s + 1; n = strlen(s + 1); int k = 0; for (int i = 1; i <= n; ++i) k = (10ll * k + s[i] - '0') % pp; ll ans = (pow_mod(1 + sqrt2, k) - pow_mod(1 - sqrt2, k)) * pow_mod(2 * sqrt2, p - 2) % p; cout << (ans + p) % p << "\n"; return 0; }
|