140. Fast Power

Calcule un% ba ^ n \% ba n% b donde a, byn son todos enteros no negativos de 32 bits.
Muestra

Por ejemplo, 231% 3 = 2

Por ejemplo, 1001000% 1000 = 0
desafío

O (registro)

 

 

 

long long fastPower2 (int a, int b, int n)
{     if (n == 0)     {         return 1% b;     }     if (n == 1)     {         return a% b;     }     si (n% 2 == 0)     {         n = n / 2;         long long i = fastPower2 (a, b, n);         long long j = i * i% b;         return j;     }     más     {         n = (n - 1) / 2;         long long i = fastPower2 (a, b, n);         long long j = i * i% b * a% b;         return j;     } }






















int fastPower (int a, int b, int n)
{        
    long long ret = fastPower2 (a, b, n);
    return (int) ret;
}

 

Supongo que te gusta

Origin blog.csdn.net/yinhua405/article/details/109234840
Recomendado
Clasificación