人见人爱A^B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53859 Accepted Submission(s): 35959
Problem Description
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
说明: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
Author
lcy
Source
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdlib> 6 #include<string.h> 7 #include<set> 8 #include<vector> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<cmath> 13 using namespace std; 14 typedef long long ll; 15 const int mod=1000; 16 ll kuaisumi(ll a,ll b){ 17 ll ans=1; 18 while(b){ 19 if(b%2==1){ 20 ans=ans*a%mod; 21 } 22 a=a*a%mod; 23 b=b/2; 24 } 25 return ans; 26 } 27 int main(){ 28 int n,m; 29 while(~scanf("%d%d",&n,&m)){ 30 if(n==0&&m==0)break; 31 ll ans=kuaisumi(n,m); 32 printf("%lld ",ans%1000); 33 } 34 return 0; 35 }