fzu Problem 1650 A^B mod C
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> using namespace std; unsigned long long multi(unsigned long long a,unsigned long long b,unsigned long long mod) { unsigned long long ans=0; while(b) { if(b&1) { b--; ans=(ans+a)%mod; } else { b/=2; a=(a+a)%mod; } } return ans; } unsigned long long Pow(unsigned long long a,unsigned long long b,unsigned long long mod) { unsigned long long ans=1; while(b) { if(b&1) { b--; ans=multi(ans,a,mod); } else { b/=2; a=multi(a,a,mod); } } return ans; } int main() { unsigned long long a,b,n; while(cin>>a>>b>>n) { a%=n; cout<<Pow(a,b,n)<<endl; } return 0; }