Description
Input
Output
Sample Input
2 3 12 6 6789 10000 0 0
Sample Output
8 984 1
题目大意:给出A和B,求出A^B的最后三位数字是什么?
分析:似乎原有的思路是直接硬上,每次取模即可。我用的是快速幂方法。感觉没啥好说的,就是一个取模运算的性质。
上代码:
#include<iostream>
#include<algorithm>
using namespace std;
int q_pow( int a, int b, int c )
{
int res=1;
while(b)
{
if(b & 1)
res = (res*a)%c;
b /= 2;
a = (a*a) % c;
}
return res;
}
int main()
{
int a, b;
while(cin >> a >> b&&a&&b)
{
cout << q_pow( a, b, 1000 ) << endl;
}
}原文地址:http://blog.csdn.net/maxichu/article/details/45642937