Wednesday 4 January 2017

Power Funtion with Complexity (log(n))

Program:-

#include <bits/stdc++.h>
using namespace std;

#define P 1000000007

long long mypow(long long x,long long y) {
    long long R = 1;
    while (y) {
        if (y&1) {
            R = (R * x) % P;
        }
        y/=2;
        x = (x * x) % P;
    }
    return R;
}

int main() {
    long int a,b;
    cin>>a>>b;
    cout<<mypow(a,b);
    return 0;
}

No comments:

Post a Comment