http://acm.hdu.edu.cn/showproblem.php?pid=2035
#include <iostream>
using namespace std;
int _pow(int a, int b)
{
long long sum(1);
a %= 1000;
while(b > 1)
{
if(b % 2 == 1) sum = (sum * a) % 1000;
a = a * a % 1000;
b /= 2;
}
return sum * a % 1000;
}
int main()
{
int a,b;
while(cin >> a >> b)
{
if(a == 0 && b == 0) break;
cout << _pow(a,b) << endl;
}
return 0;
}
二分求幂算法 http://blog.csdn.net/prstaxy/article/details/8740838
pow() logn。