Answers for "modular exponentiation"

1

binary exponentiation modulo m

long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
Posted by: Guest on September-03-2020
0

modular exponentiation

long long int PowMod(long long int x,long long int n,long long int M)
		{
		    if(n<=1)
		        return x%M;
		   long long int res;
		    res=PowMod(x,n/2,M)%M;
		    res=(res*res)%M;
		    if(n%2==1)
		        res=((res%M)*(x%M))%M;
		    return res%M;
		}
Posted by: Guest on August-23-2021

Browse Popular Code Answers by Language