HDOJ 2035 人见人爱A^B

人见人爱A^B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13930    Accepted Submission(s): 9852


Problem Description
求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
 
这题我就是想练练快速幂的 ,其实很简单
 1 //快速幂取模----其实就是将指数用二进制表示下根据每位上是 0 还是 1 进行运算
 2 #include <iostream>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int mod = 1000;
 8 int main()
 9 {
10     int a, b;
11     while(scanf("%d%d", &a, &b) && (a || b))
12     {
13         int res = 1;
14         while(b)
15         {
16             if(b & 1)   // 看b 的二进制位的最后一位是否为1
17                 res = res * a % mod;
18             a = a * a % mod;
19             b >>= 1;
20         }
21         printf("%d\n", res);
22     }
23     return 0;
24 }
功不成,身已退
原文地址:https://www.cnblogs.com/dongsheng/p/2797441.html