Need help for a fibonacci problem

Revision en3, by backupkid, 2026-05-25 10:57:59

Hello reader!

I came across this problem and I'm not sure how to approach it. Any hints would be appreciated!

Problem:

The Fibonacci sequence is defined as F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2). So: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Given N, count the number of ways to write N as a product of Fibonacci numbers, where each factor is > 1. Order does not matter (2×4 and 4×2 are the same representation).

Example: Input: 4 2 7 40 64 Output: 1 0 2 3

Constraints: - T ≤ 50 test cases - N can be up to 10^18

I have no idea where to start. How do you approach this kind of problem?

Thank you!

update : the problem was solved

solotion 1: Top-Down DP with Memoization author MEDAAA

Idea
Code

solution 2: Greedy + Math author:phsads

Idea
Code

Complexity Comparison

Approach Time per Query Space Notes
DP Memoization $$$O(\text{Fib}^2 \log N)$$$ $$$O(\text{Fib} \log N)$$$ More intuitive, general DP
Greedy + Math $$$O(\log N)$$$ $$$O(\text{Fib})$$$ Faster, uses number theory

Credits

Thanks to MEDAAA, phsads, Ahmed-Nawaz, InfiniteLoops1730, VladiG, Metall1cA, helpT27, and others for all the discussions and ideas in the comments. Really appreciate the different approaches and insights — from DP attempts to number theory explanations.

Tags fibonacci

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English backupkid 2026-05-25 11:22:30 29 Tiny change: ' Credits\n\nThanks t' -> ' Credits\ncan u give me contribution :)\nThanks t'
en4 English backupkid 2026-05-25 11:07:55 2 Tiny change: '[user:helpT27,2026-0' -> '[user:helperT27,2026-0'
en3 English backupkid 2026-05-25 10:57:59 6655
en2 English backupkid 2026-05-24 19:04:50 12 Tiny change: '\nHello Codeforces!\n\nI cam' -> 'Hello reader!\n\nI cam'
en1 English backupkid 2026-05-24 19:00:22 681 Initial revision (published)