how to calculate 2^n modulo 1000000007 , n = 10^9

前端 未结 3 1823
傲寒
傲寒 2020-12-11 06:58

what is the fastest method to calculate this, i saw some people using matrices and when i searched on the internet, they talked about eigen values and eigen vectors (no idea

3条回答
  •  醉话见心
    2020-12-11 07:45

    f(n) = (2*f(n-1)) + 2 with f(1)=1
    

    is equivalent to

    (f(n)+2) = 2 * (f(n-1)+2)
             = ...
             = 2^(n-1) * (f(1)+2) = 3 * 2^(n-1)
    

    so that finally

    f(n) = 3 * 2^(n-1) - 2
    

    where you can then apply fast modular power methods.

提交回复
热议问题