题目:http://acm.hdu.edu.cn/showproblem.php?pid=1163
输入n,求n的n次方的九余数。
思路:将快速幂和九余数定理结合一下。
#include<iostream>
#include<cmath>
using namespace std;
int mode_xp(int a, int b, int n)
{
long long ret = 1, temp;
temp = a;
while (b)
{
if (b & 1)
ret = ret*temp%n;
temp = temp*temp%n;
b >>= 1;
}
return ret;
}
int main()
{
int n,ans;
while (cin >> n&&n!=0)
{
ans = mode_xp(n, n, 9);
if (ans == 0)
ans = 9;
cout << ans << endl;
}
return 0;
}