Hey there,
I just took a programming contest and received a pretty interesting mathematical problem.
To sum up, you are given two numbers n and k (1 <= n <= 10^18, 1 <= k <= 10^6).
Assume that F(i) is the i-th number in the Fibonacci sequence
E.g: F(0) = 0, F(1) = 1, F(2) = 1, F(3) = 2, e.t.c.
Calculate the sum S of the first n Fibonacci numbers raised to the power of k.
Or in terms of pseudocode:
S = 0
for (i = 1 -> n)
S = S + F(i)^k
Print the answer modulo 998244353. Is there any good approach to this problem? Thank you for reading!









Do you have a link to the task? I'd like to try a few things
I only have the pdf link, but here you go, you can try
https://icpc.hcmus.edu.vn/public/problems/37/text
Says ‘internal server error’. It looks like an interesting problem, it’d be great if you could find a link to an online judge of sorts :)
https://oj.vnoi.info/problem/icpc23_mn_f
You can try to find polinom with degree k. You take first k Fibonacci and build Lagrange or Newton polinom. Then find F(n). Tbh I'm not sure if it will work but I think it will. Similar problem was here
Pretty sure this does not work because unlike $$$\sum_{x = 0}^{n} x^{k}$$$, the result of this sum $$$\sum_{x = 0}^{n} F^{k}(x)$$$ is not a polynomial of any degree. Even when $$$k = 1$$$ it is well known that $$$\sum_{x = 0}^{n} F(x) = F(n + 2) - 1$$$ is not a polynomial. Unless there is some generalization of Lagrange interpolation that I am not aware of, I don't think it's possible to solve it this way (would love to be proven wrong!)
From Binet's formula, we know that
, where $$$\phi = \frac{1 + \sqrt 5}{2}$$$ and $$$\psi = \frac{1 - \sqrt 5}{2}$$$.
Using that, let's expand $$$F_n^k$$$ using Newton's binomial theorem:
Next, let's expand $$$S$$$:
Note that for each fixed $$$i$$$, the only sum we need to evaluate is $$$\sum_{j=1}^n A_i^j$$$, where $$$A_i = \phi^i \psi^{k-i}$$$. This problem has a well-known solution with complexity $$$\mathcal O(\log n)$$$, see ABC 293E. Therefore, we obtained an algorithm with a time complexity of $$$\mathcal O(k \log n)$$$.
At this point, we only need to evaluate $$$\sqrt 5$$$ modulo $$$p$$$, then we can use it to directly evaluate $$$(\sqrt 5)^k$$$, $$$\phi$$$, $$$\varphi$$$, and $$$A_i$$$. In ZOJ-3774, $$$p = 10^9 + 9$$$, and "fortunately" such number exist: $$$383008016^2 \equiv 5 \pmod {10^9 + 9}$$$, so $$$383008016 \equiv \sqrt 5 \pmod {10^9 + 9}$$$. There are other unfortunate value of $$$p$$$ however, and $$$998244353$$$ is one of them (To determine whether a value of $$$p$$$ is fortunate or not, see Legendre symbol).
So how do we solve this? We simply does not care about evaluating $$$\sqrt 5$$$ at all!. Instead, we will work with numbers in the extension field of $$$\mathbb Z_p$$$:
We will denote $$$a + b \sqrt 5$$$ as $$$\langle a, b \rangle$$$. So
Arithmetic operations can still be performed on these numbers. You can try to figure it out yourself — addition, subtraction and multiplication should be easy enough, but if you got stuck on division, here is a hint:
By the way, another solution for the division is that in $$$\mathbb{F}_{p^2} = \mathbb{F}_{p}\left(\sqrt{5}\right)$$$, we always have $$$a^{p^2} \equiv a \pmod{p^2}$$$.
So, if $$$a \neq \left \lt 0, 0\right \gt $$$, then $$$a^{p^2 - 1} \equiv \left \lt 1, 0\right \gt \pmod p$$$, and of course, $$$\dfrac{1}{\left \lt c, d\right \gt } \equiv \left \lt c, d\right \gt ^{p^2 - 2} \pmod p$$$