Блог пользователя backupkid

Автор backupkid, история, 4 месяца назад, По-английски

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

can u give me contribution :) Thanks to MEDAAA, phsads, Ahmed-Nawaz, InfiniteLoops1730, VladiG, Metall1cA, HelperT27, 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.

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

»
4 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

imo you could do something like first discretize every factor of N and then define dp[i] as the number of ways to do i

you can brute force the fibonaccis, as there's less than 60 below 10^18

»
4 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

We know that the number of divisors of $$$N$$$ is $$$\mathcal{O}(N^\frac13)$$$.

So, you can define $$$dp_i$$$ to be the number of ways to form $$$i$$$ as a product of fibonacci numbers. Note that the number of states is $$$\approx 10^6$$$. The base case will be $$$dp_1 = 1$$$ and the transitions will be $$$dp_i = \sum_{F_j \mid i} dp_{i / F_j}$$$. The answer will be stored in $$$dp_N$$$.

The complexity will depend on your implementation but assuming you will not use a map the complexity will be $$$\mathcal{O}(N^\frac13 \cdot M)$$$ where $$$M$$$ is the count of fibonacci numbers $$$\leq 10^{18}$$$, which is bounded by $$$90$$$ I think.

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Let ans be an answer to your problem. Start by building the Fibonacci sequence up till the value is lower than N. At the same time check whether N can be divided by F(x). If you can divide it then add 1 to ans. The final answer is ans/2 because order of factors doesn't matter.

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Every fibonacci number between $$$1$$$ and $$$10^{18}$$$ except for $$$F_{6} = 8$$$ and $$$F_{12} = 144$$$ has at least one prime factor that doesn't appear in any earlier fibonacci number.

This means that we can do the following greedy algorithm:

Iterate from the largest $$$F_{i}$$$ that is below $$$10^{18}$$$ to the smallest (except for the $$$2$$$,$$$3$$$,$$$8$$$,$$$144$$$), then divide it by the highest power of $$$F_{i}$$$ possible.

If the result after the greedy algorithm isnt in the form of $$$2^{p}3^{q}$$$, then the answer is $$$0$$$.

Otherwise the result can be calculated $$$O(log n)$$$ using some janky math.

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by backupkid (previous revision, new revision, compare).

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can you give a link to the problem?