pow using device and conquer

x^n

x^8 = x^4 * x^4 x^4 = x^2 * x^2 x^2 = x * x

if x is even x^n = x^n/2 * x^n/2 else x^n = x^(n-1)/2 * x^(n-1)/2 * x

가짜 소수: https://www.acmicpc.net/problem/4233

pow using devide and conquer

ll mod_pow(ll x, ll n, ll mod){
    ll res = 1; 
    while (n){
        if (n&1) res = res*x%mod; // if n is odd 
        x = x*x%mod;  
        n >>= 1;  // same as n /= 2;  
    }
    return res;  
}
 

소수판별

bool isPrime(int n){
    for (int i = 2; i*i <= n; i++){
        if (n%i == 0) 
            return false; 
    }
    return n != 1;  
}