Fibonacci Fever#12483

Fibonacci Fever (Çözgüdiň düşündirişi)

Since can be as large as 10^{18}, iterating over the Fibonacci numbers one by one is impossible. The matrix method does not work either: the sum of -th powers would require a matrix of size about , and can be as large as 10^5. Therefore, we use an explicit formula for the Fibonacci numbers — Binet's formula:

f_i = \frac{\varphi^i - \psi^i}{\sqrt{5}},

where

\varphi = \frac{1 + \sqrt{5}}{2}, \quad \psi = \frac{1 - \sqrt{5}}{2}

The numbers and are the roots of the equation x^2 = x + 1, and by Vieta's formulas \varphi\psi = -1.

Expanding the power. Raise Binet's formula to the power and expand it using the binomial theorem:

f_i^k = \frac{(\varphi^i - \psi^i)^k}{(\sqrt{5})^k} = \frac{1}{(\sqrt{5})^k}\sum_{j=0}^{k}(-1)^j C_k^j\, \varphi^{i(k-j)}\psi^{ij} = \frac{1}{(\sqrt{5})^k}\sum_{j=0}^{k}(-1)^j C_k^j\, \left(\varphi^{k-j}\psi^j\right)^i

Denote

t_j = \varphi^{k-j}\psi^j, \quad j = 0, 1, \ldots, k

Sum over from to and change the order of summation:

S = \sum_{i=1}^{n} f_i^k = \frac{1}{(\sqrt{5})^k}\sum_{j=0}^{k}(-1)^j C_k^j \sum_{i=1}^{n} t_j^{\,i}

Thus, the sum of terms is reduced to a sum of k + 1 terms.

Geometric progression. The inner sum is the sum of a geometric progression with common ratio t = t_j:

G(t) = \sum_{i=1}^{n} t^i = \begin{cases} \dfrac{t\,(t^n - 1)}{t - 1}, & t \neq 1, \\[2mm] n, & t = 1 \end{cases}

The case t = 1 really occurs and must be handled separately (otherwise a division by zero occurs). For example, if is divisible by , then for j = k / 2 we get

t_{k/2} = (\varphi\psi)^{k/2} = (-1)^{k/2} = 1

The power t^n is computed by binary exponentiation in O(\log n) time, so each term is found quickly even for n = 10^{18}.

Computing the ratios iteratively. There is no need to raise and to a power anew for each . Consecutive ratios are related by

t_{j+1} = t_j \cdot \frac{\psi}{\varphi},

so it is enough to compute t_0 = \varphi^k once, and then multiply the current value by at each iteration.

Modular computations. Floating-point arithmetic is not suitable here: we need the exact answer modulo p = 10^9 + 7. We will work with numbers of the form a + b\sqrt{5}, where and are residues modulo . Addition and multiplication of such numbers follow the rules

(a + b\sqrt{5}) + (c + d\sqrt{5}) = (a + c) + (b + d)\sqrt{5}, \\ (a + b\sqrt{5})(c + d\sqrt{5}) = (ac + 5bd) + (ad + bc)\sqrt{5}

Numbers of the form a + b\sqrt{5} form the field \mathbb{Z}_p[\sqrt{5}]. In this field, every nonzero element is invertible:

(a + b\sqrt{5})^{-1} = \frac{a - b\sqrt{5}}{a^2 - 5b^2},

where the denominator a^2 - 5b^2 is nonzero modulo . This guarantees that all divisions in the solution are valid. Division by in the formulas for and is replaced by multiplication by 2^{-1} \bmod p = (p + 1) / 2.

The inverse of 2. The inverse of modulo is a number such that 2x \equiv 1 \pmod{p}.

Let us check that x = (p + 1) / 2 works:

Hence, (p + 1) / 2 is exactly 2^{-1} \bmod p.

The required sum is an integer, so in the final value a + b\sqrt{5} the coefficient is necessarily zero, and the answer is .

Example

Consider the first example: , \boldsymbol{k = 1}. For k = 1 the formula becomes

S = \frac{1}{\sqrt{5}}\left(\sum_{i=1}^{4}\varphi^i - \sum_{i=1}^{4}\psi^i\right)

Let us compute the powers of using the relation \varphi^2 = \varphi + 1:

\varphi = \frac{1 + \sqrt{5}}{2}, \quad \varphi^2 = \frac{3 + \sqrt{5}}{2}, \quad \varphi^3 = \frac{4 + 2\sqrt{5}}{2}, \quad \varphi^4 = \frac{7 + 3\sqrt{5}}{2}

Their sum equals \dfrac{15 + 7\sqrt{5}}{2}. The powers of are obtained by replacing \sqrt{5} with -\sqrt{5}, so their sum equals \dfrac{15 - 7\sqrt{5}}{2}. Therefore,

S = \frac{1}{\sqrt{5}}\left(\frac{15 + 7\sqrt{5}}{2} - \frac{15 - 7\sqrt{5}}{2}\right) = \frac{7\sqrt{5}}{\sqrt{5}} = 7,

which matches the answer: 1 + 1 + 2 + 3 = 7.

Consider an example for \boldsymbol{k = 3}. Expand the cube of the difference:

f_i^3 = \frac{(\varphi^i - \psi^i)^3}{(\sqrt{5})^3} = \frac{\varphi^{3i} - 3\varphi^{2i}\psi^i + 3\varphi^i\psi^{2i} - \psi^{3i}}{5\sqrt{5}}

The common ratios of the progressions are (using \varphi\psi = -1):

t_0 = \varphi^3, \quad t_1 = \varphi^2\psi = -\varphi, \quad t_2 = \varphi\psi^2 = -\psi, \quad t_3 = \psi^3

Then

\sum_{i=1}^{n} f_i^3 = \frac{1}{5\sqrt{5}}\left(\sum_{i=1}^{n}(\varphi^3)^i - 3\sum_{i=1}^{n}(-\varphi)^i + 3\sum_{i=1}^{n}(-\psi)^i - \sum_{i=1}^{n}(\psi^3)^i\right)

Each of the four sums is computed using the geometric progression formula. Let us check this for n = 3. The powers of are

\varphi^3 = 2 + \sqrt{5}, \quad \varphi^6 = 9 + 4\sqrt{5}, \quad \varphi^9 = 38 + 17\sqrt{5}

Then

\sum_{i=1}^{3}(\varphi^3)^i = \varphi^3 + \varphi^6 + \varphi^9 = (2 + \sqrt{5}) + (9 + 4\sqrt{5}) + (38 + 17\sqrt{5}) = 49 + 22\sqrt{5}, \\ \sum_{i=1}^{3}(-\varphi)^i = -\varphi + \varphi^2 - \varphi^3 = \frac{-(1 + \sqrt{5}) + (3 + \sqrt{5}) - (4 + 2\sqrt{5})}{2} = -1 - \sqrt{5}

The sums for are obtained by replacing \sqrt{5} with -\sqrt{5}:

\sum_{i=1}^{3}(-\psi)^i = -1 + \sqrt{5}, \quad \sum_{i=1}^{3}(\psi^3)^i = 49 - 22\sqrt{5}

Substituting the values found:

\sum_{i=1}^{3} f_i^3 = \frac{(49 + 22\sqrt{5}) - 3(-1 - \sqrt{5}) + 3(-1 + \sqrt{5}) - (49 - 22\sqrt{5})}{5\sqrt{5}} = \frac{50\sqrt{5}}{5\sqrt{5}} = 10

The result matches the direct computation:

f_1^3 + f_2^3 + f_3^3 = 1^3 + 1^3 + 2^3 = 1 + 1 + 8 = 10

Consider an example for \boldsymbol{k = 4} (a special case). Here

t_0 = \varphi^4, \quad t_1 = \varphi^3\psi = -\varphi^2, \quad t_2 = \varphi^2\psi^2 = 1, \quad t_3 = \varphi\psi^3 = -\psi^2, \quad t_4 = \psi^4

and

\sum_{i=1}^{n} f_i^4 = \frac{1}{25}\Biggl(\sum_{i=1}^{n}(\varphi^4)^i - 4\sum_{i=1}^{n}(-\varphi^2)^i + 6n - 4\sum_{i=1}^{n}(-\psi^2)^i + \sum_{i=1}^{n}(\psi^4)^i\Biggr)

The middle term corresponds to the common ratio t_2 = 1: the geometric progression formula does not apply to it, and the sum 1 + 1 + \ldots + 1 equals . This is exactly the case handled separately in the program. For example, for n = 3 we get:

f_1^4 + f_2^4 + f_3^4 = 1^4 + 1^4 + 2^4 = 1 + 1 + 16 = 18

Algorithm implementation

Declare a constant for the modulus .

#define MOD 1000000007

We'll store the values of binomial coefficients in the array .

vector<long long> Cnk;

To implement arithmetic in the field Z_{MOD} [\sqrt{5}], define a structure . This structure represents numbers of the form x + y \sqrt{5}.

struct Num
{
  long long x, y;
  Num(long long x = 0, long long y = 0) : x(x% MOD), y(y% MOD) {}
};

The function add implements addition in the field Z_{MOD} [\sqrt{5}].

Num add(const Num& a, const Num& b)
{
  return Num((a.x + b.x) % MOD, (a.y + b.y) % MOD);
}

The function mult implements multiplication in the field Z_{MOD} [\sqrt{5}].

Num mult(const Num& a, const Num& b)
{
  return Num(
    (a.x * b.x + 5 * a.y % MOD * b.y) % MOD,
    (a.x * b.y + a.y * b.x) % MOD
  );
}

The function pw implements binary exponentiation in the field Z_{MOD} [\sqrt{5}], where the exponent is an integer.

Num pw(Num base, long long exp)
{
  Num res(1, 0);
  while (exp)
  {
    if (exp & 1) res = mult(res, base);
    base = mult(base, base);
    exp >>= 1;
  }
  return res;
}

The function pw implements fast binary exponentiation to compute a^b modulo .

long long pw(long long a, long long b)
{
  long long res = 1;
  a %= MOD;
  while (b)
  {
    if (b & 1) res = (res * a) % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

The function inv computes the multiplicative inverse of an element in the field Z_{MOD} [\sqrt{5}].

If

a = x + y \sqrt{5},

then its inverse element is given by

a^{-1} = \frac{1}{x+y\sqrt{5}} = \frac{x-y\sqrt{5}}{x^2-5y^2}
Num inv(const Num& a)
{
  long long den = (a.x * a.x - 5 * a.y * a.y) % MOD;
  if (den < 0) den += MOD;

  long long invDen = pw(den, MOD - 2);

  return Num(
    a.x * invDen % MOD,
    (MOD - a.y) * invDen % MOD
  );
}

The function divide implements division in the field Z_{MOD} [\sqrt{5}]:

\frac{a}{b} = a \cdot b^{-1}
Num divide(const Num& a, const Num& b)
{
  return mult(a, inv(b));
}

The main part of the program. Read the input data.

scanf("%lld %lld", &n, &k);

Compute the binomial coefficients C_k^0, C_k^1, C_k^2, ..., C_k^k and store them in the array . Note that

Cnk[i] = C_k^i = C_k^{i-1} \cdot \frac{k - i + 1}{i}
Cnk.resize(k + 1);
Cnk[0] = 1;
for (i = 1; i <= k; i++)
{
  Cnk[i] = Cnk[i - 1] * (k - i + 1) % MOD;
  Cnk[i] = Cnk[i] * pw(i, MOD - 2) % MOD;
}

Compute inv2 = 2^{-1} \mod MOD = (MOD + 1) / 2.

long long inv2 = (MOD + 1) / 2;

Initialize the constants: phi = \phi = \frac{1 + \sqrt{5}}{2} and psi = \psi = \frac{1 - \sqrt{5}}{2}.

Num phi(inv2, inv2);
Num psi(inv2, MOD - inv2);

Compute ratio = \psi / \phi.

Num ratio = divide(psi, phi);

Initialize the constants and in the field Z_{MOD} [\sqrt{5}].

Num one(1, 0);
Num minus_one(MOD - 1, 0);

The answer will be accumulated in the variable .

Num res(0, 0);

Initialize t = \phi^k. Then, in the loop, we will multiply this value by , obtaining

\phi^k, \phi^{k-1}\psi, \phi^{k-2}\psi^2, ..., \phi\psi^{k-1}, \psi^k
Num t = pw(phi, k);

In the variable we compute the sum

\sum_{i=1}^{n} f_i^k = \frac{1}{(\sqrt{5})^k} \sum_{j=0}^{k} (-1)^j C_k^j \sum_{i=1}^{n} \left(\phi^{k-j}\psi^j\right)^i

The required sum contains k + 1 terms. Therefore, we perform k + 1 iterations.

for (j = 0; j <= k; j++)
{
  Num term;

At this point t = \phi^{k-j} \psi^j. Compute the sum of the geometric progression:

term = \sum_{i=1}^{n} t^i = \frac{t(t^n-1)}{t-1}

However, the case t = 1 should be handled separately, because the formula for the sum of a geometric progression contains division by . When t = 1 the sum takes the form: 1 + 1 + 1 + ... + 1 ( times) and is equal to .

  if (t.x == 1 && t.y == 0)
    term = Num(n % MOD, 0);
  else
  {
    Num tn = pw(t, n);
    Num num = add(tn, minus_one);     // t^n - 1
    term = mult(t, num);              // t * (t^n - 1)
    Num den = add(t, minus_one);      // t - 1
    term = divide(term, den);         // t * (t^n - 1) / (t – 1)
  }

Compute term = (-1)^j C_k^j \cdot term.

  long long coef = Cnk[j];
  if (j & 1) coef = MOD - coef;

  term.x = term.x * coef % MOD;
  term.y = term.y * coef % MOD;

Add the next term to the result .

  res = add(res, term);

Multiply by , obtaining t = \phi^{k-j-1} \psi^{j + 1}.

  t = mult(t, ratio);
}

It remains to divide by (\sqrt{5})^k.

Num sqrt5(0, 1);
Num sqrt5k = pw(sqrt5, k);
res = divide(res, sqrt5k);

Print the answer.

printf("%lld\n", res.x);