Pow with mod huge

llu modpow(llu base, llu exp, llu modulus) {
  base %= modulus;
  llu result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

int main()
{
    /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif*/
    fast

    llu a,b=1000000000+7,c=2;
    cin>>a;
    cout<<modpow(c,a,b)<<endl;
}

https://stackoverflow.com/questions/8496182/calculating-powa-b-mod-n

Comments

Popular posts from this blog

C++ STL practice problem link

Binary Index Tree(BIT)

Combinatorics