Problem: Find the coefficient of xk in (1−x−x2)-6 modulo m.
Constraints:
k≤264
m≤105, m can be a composite number.
I have 10^5 such queries to process in 2 sec, so O(log k) for each query solution will work.
kth Fibonacci and Lucas numbers can be calculated in O(log k).
My Approach: I thought that closed form expression may not be possible(I have not figured out any idea till now also), but some recursive formula or algorithm may be possible.
What I have found:
On OEIS , I came to know that this reciprocal polynomial is generating function of convolved Fibonacci number of order 6 .
Then I have found this paper https://cs.uwaterloo.ca/journals/JIS/VOL7/Moree/moree12.pdf
in which required answer is represented as sum of Fibonacci and Lucas numbers divided by powers of 5.
This is problematic since m can be a composite number(multiple of 5). I am stuck here because multiplicative inverse of powers of 5 will not exist in that case.
Please suggest me any solution for this or any other way to calculate the answer.
Thanks in advance.
I think you've almost got it. You just need to be careful about how you clear denominators.
For example, the paper you linked to gives the identity
$$50F^{(3)}_j = 5(j + 1)(j + 2)F_{j+3} + 6(j + 1)L_{j+2} + 12F_{j+1} \, .$$
In order to compute, $F^{(3)}_j \bmod m$, start by computing the quantity $$Q=\left(5(j + 1)(j + 2)F_{j+3} + 6(j + 1)L_{j+2} + 12F_{j+1}\right) \bmod 50m \, .$$
By construction, we know that $Q$ is the unique integer with $0 \leq Q < 50m$ such that $Q+50mk=50F^{(3)}_j$ for some integer $k$. By dividing these two relations through by $50$, it follows that $Q$ is a multiple of $50$, that $0 \leq Q/50 < m$, and that $Q/50+mk=F^{(3)}_j$. But this precisely means that $Q/50 = F^{(3)}_j \bmod m$.
You'll be able to do the same thing to get $F^{(6)}_j$; you'll just have to divide by a larger number (namely $5^5(5!)=375000$ instead of $5^2(2!)=50$). But it shouldn't be too large for your purposes, I think...