Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

problem with Modular exponentiation

$
0
0

Consider a code for Modular exponentiation assume we are calculating (x^n)% MOD

while(n>0)
{
    if(n&1)
    result = (result*x)%MOD;
    cout<<result<<"\n";
    x=(x*x)%MOD;

    n=n>>1;
}

suppose x = 4 and n is large say 100 and MOD is 10^9 +6

so my program has overflow and giving result in -ve number so in this program x can be very large < MOD and when we multiply it be result intermediate value is very large so how to do it
p.s i am newbie :)


Viewing all articles
Browse latest Browse all 39796

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>