HDU1163 快速幂+九余数

来源:互联网 发布:c 常量数组 编辑:程序博客网 时间:2024/06/01 08:52

题目: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;}