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

前端 未结 3 1808
傲寒
傲寒 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:44

    Modular exponentiation by the square-and-multiply method:

    function powerMod(b, e, m)
        x := 1
        while e > 0
            if e%2 == 1
                x, e := (x*b)%m, e-1
            else b, e := (b*b)%m, e//2
        return x
    

提交回复
热议问题