What is the fastest way to compute large power of 2 modulo a number

前端 未结 5 2159
南方客
南方客 2020-12-06 02:48

For 1 <= N <= 1000000000, I need to compute 2N mod 1000000007, and it must be really fast!
My current approach i

5条回答
  •  再見小時候
    2020-12-06 03:48

    If u also want to store that array ie. (2^i)%mod [i=0 to whatever] than:

    long mod = 1000000007;
    long int pow_mod[ele]; //here 'ele' = maximum power upto which you want to store 2^i
    pow_mod[0]=1; //2^0 = 1
    for(int i=1;i

    I hope it'll be helpful to someone.

提交回复
热议问题