Description:
给定A和B,求A^B的最后三位数。
Input:
每行两个正整数A和B(1<=A,B<=10000),如果A=0, B=0,表示输入数据的结束。
Output:
A^B的最后三位数。
Sample Input:
2 3
12 6
6789 10000
0 0
Sample Output:
8
984
1
Reference Code:
#include<cstdio>
int qp(int a,int b,int c){
int ans=1;
int base=a%c;
while(b){
if (b&1) ans=ans*base%c;
base=base*base%c;
b>>=1;
}
return ans;
}
int main(){
int a,b;
while(scanf("%d %d",&a,&b) && a && b){
printf("%d\n",qp(a,b,1000));
}
}