backupkid's blog

By backupkid, history, 61 minute(s) ago, In English

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!

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
45 minutes ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    43 minutes ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    welll i dont think if it that simple i thought this way but didnt find something with proof could you briefly explain

    • »
      »
      »
      39 minutes ago, hide # ^ |
      Rev. 2  
      Vote: I like it +3 Vote: I do not like it

      ok so first

      you take every factor and discretize them (if you don't know what discretize it, it means assigning number), at most sqrt(N) * 2 because of that one thing i forgot

      next you can use dp, where dp[i] = number of ways to do i and you change by going dp[x*i] += dp[i] (assuming x is fibonacci)

      and becuase fibs are exponentical this should take a short time

      I could make code if you want

      edit: i'm stupid

      • »
        »
        »
        »
        37 minutes ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        well sqrt(N) * 2 in worst case equals 2e9 so not possible

        • »
          »
          »
          »
          »
          30 minutes ago, hide # ^ |
          Rev. 2  
          Vote: I like it +1 Vote: I do not like it

          according to the orange guy there is at most cbrt(N) so it's fine

          I don't know how you could calulate that

          edit: FUN FACT, there are 116 primes numbers in the total facrotization of the fib numbers, so you could do that

          • »
            »
            »
            »
            »
            »
            23 minutes ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            how do you get prime factorization of some specific fib number or how do you get those 116 primes??

            • »
              »
              »
              »
              »
              »
              »
              21 minute(s) ago, hide # ^ |
               
              Vote: I like it 0 Vote: I do not like it
              • »
                »
                »
                »
                »
                »
                »
                »
                18 minutes ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                yeah ok fine. you win

  • »
    »
    18 minutes ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    we only need 42 fibonacci numbers, as we only care about fibonacci numbers <= 1e9(edit: this is incorrect, we need all fib numbers upto 1e18). We can do probably do a memoized dp because we only care about many states. We can keep dividing N by each fibonacci number as many times as we can.

»
33 minutes ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

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.