N
N
NikSIk312019-05-30 17:59:48
Algorithms
NikSIk31, 2019-05-30 17:59:48

What is the name of the algorithm?

What is the name of the algorithm and why is there a class template here, why is it ???

template< class T >
int pow_mod(T a, T b, T m) {
    T r = 1;
    a %= m;
    while (b) {
        if (b & 1)
            r = (r * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return r;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ihor Bratukh, 2019-05-30
@BRAGA96

Google template c++
So that the function can take arguments of different types. Instead of overloading function arguments for different types (float, int), a template is used here.

J
jcmvbkbc, 2019-05-30
@jcmvbkbc

What is the name of the algorithm?

Fast exponentiation of a to the power of b. modulo m.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question