# 51nod 1236 序列求和 V3

Fib(n)表示斐波那契数列的第n项，Fib(n) = Fib(n-1) + Fib(n-2)。Fib(0) = 0, Fib(1) = 1。

(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)

F(n, k) = Fib(n)^k（Fib(n)的k次幂）。
S(n, k) = F(1, k) + F(2, k) + ...... F(n, k)。

# 输出

$$f(i)=\frac{1}{\sqrt 5}\left[\left(\frac{1+\sqrt 5}{2}\right)^i-\left(\frac{1-\sqrt 5}{2}\right)^i\right]$$

\begin{aligned} &\sum_{i=1}^{n}\left\{\frac{1}{\sqrt 5}\left[\left(\frac{1+\sqrt 5}{2}\right)^i-\left(\frac{1-\sqrt 5}{2}\right)^i\right]\right\}^k \\ =&\left(\frac{1}{\sqrt 5}\right)^k\sum_{i=1}^{n} \sum_{j=0}^{k} {k \choose j} a^{ij}b^{i(k-j)}(-1)^{k-j} \\ =&\left(\frac{1}{\sqrt 5}\right)^k \sum_{j=0}^{k} {k \choose j} (-1)^{k-j} \sum_{i=1}^{n}\left(a^{j}b^{k-j}\right)^i \\ \end{aligned}

$$\left(\frac{1}{\sqrt 5}\right)^k \sum_{j=0}^{k} {k \choose j} (-1)^{k-j} \frac{q^{n+1}-q}{q-1}$$

1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4
5 const int mod = 1e9 + 9;
6
7 const int s5 = 616991993;
8
9 ll pw(ll a, ll b) {
10     ll res = 1;
11     for( ; b ; b >>= 1, a = a * a % mod)
12         if(b & 1)
13             res = res * a % mod;
14     return res;
15 }
16
17 const int N = 1e5 + 10;
18
19 ll fac[N], invfac[N];
20
21 ll C(int n, int m) {
22     return n < m ? 0 : fac[n] * invfac[m] % mod * invfac[n - m] % mod;
23 }
24
25 const ll a = (1 + s5) * pw(2, mod - 2) % mod;
26 const ll b = (1 - s5) * pw(2, mod - 2) % mod;
27
28 void sol() {
29     ll n, k; cin >> n >> k;
30     ll ans = 0;
31     for(int j = 0 ; j <= k ; ++ j) {
32         ll q = pw(a, j) * pw(b, k - j) % mod;
33         if(q == 1) {
34             ans += C(k, j) * pw(-1, k - j) % mod * (n % mod) % mod;
35         } else {
36             ans += C(k, j) * pw(-1, k - j) % mod * ((pw(q, n + 1) - q) % mod * pw(q - 1, mod - 2) % mod) % mod;
37         }
38         ans %= mod;
39     }
40     ans = ans * pw(pw(s5, mod - 2), k) % mod;
41     cout << (ans % mod + mod) % mod << endl;
42 }
43
44 int main() {
45     ios :: sync_with_stdio(0);
46     const int T = 1e5;
47     fac[0] = invfac[0] = 1;
48     for(int i = 1 ; i <= T ; ++ i) fac[i] = fac[i - 1] * i % mod;
49     invfac[T] = pw(fac[T], mod - 2);
50     for(int i = T - 1 ; i >= 1 ; -- i) invfac[i] = invfac[i + 1] * (i + 1) % mod;
51     int fafa; cin >> fafa;
52     while(fafa --) sol();
53 }
51nod 1236 序列求和 V3

posted @ 2018-12-11 17:08  KingSann  阅读(...)  评论(...编辑  收藏