hoainiem's blog

By hoainiem, history, 5 weeks ago, In English

I have a problem about fibonacci numbers: ==================

let f[i] be the ith fibonacci number

f[i] = 1 (1 <= i <= 2)

f[i] = f[i-1] + f[i-2] (i > 2)

how can i calculate f[f[n]] mod M (n, M <= 10^9). anyone can help me?

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

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 weeks ago, # |
  Vote: I like it -17 Vote: I do not like it
  • »
    »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    i don't think these 2 problems are similar. your problem is calculate the nth fibonacci number , which can be solved by matrix multiplication. but we can't use it to solve my problem because f[n] can be very large, and we can't use modules because (f[f[n]]) mod m is not equal to (f[f[n]mod m] ) mod m

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      sorry, my bad I misread the problem I thought you are asking for f[n] % M.

»
5 weeks ago, # |
  Vote: I like it +4 Vote: I do not like it

I guess you can try to find pisano period of M, then calculate f[n] mod P(m), where P(i) — pisano period of modulo i, and then calculate the final answer mod M.

Though I'm not sure how to calculate pisano period for such big M, you can try to look for some optimizations here.(also google is your big friend here)

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thank you very much. i've tried to google it but i can't find any way to calculate pisano period for m <= 1^9

»
5 weeks ago, # |
  Vote: I like it -37 Vote: I do not like it