File size: 316 Bytes
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int modular_mul(int a, int b, int modulus)

{
    int res = 0;
    int flag = 1;
    if (b < 0) {
        b = -b;
        flag = -1;
    }

    while (b > 0) {
        if (b % 2 == 1) {
            res = (res + a) % modulus;
        }
        b = b / 2;
        a = (a + a) % modulus;
    }
    return res * flag;
}