Check out example codes for "exponenciacion binaria". It will help you in understanding the concepts better.

Code Example 1

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
//complexity O(log k)
ull po(ull n,ull k){
	ull x=1;
	while(k){
		if(k&1)
			x*=n;
		n*=n;
		k>>=1;
	}
	return x;
}
int main(){
	ull n,m;
    //n^m
	cin>>n>>m;
	cout<<po(n,m);
	return 0;
}

Learn ReactJs, React Native from akashmittal.com