51Nod-1004 n^n的末位数字【快速模幂】

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3


问题链接51Nod-1004 n^n的末位数字

问题分析:一个简单的快速模幂计算。

程序说明:快速模幂计算被封装到一个函数中,直接调用即可。

题记:(略)


参考链接:(略)


AC的C++程序如下:

#include <iostream>

using namespace std;

// 快速模幂计算函数
int powermod(long long a, int n, int m)
{
    long long res = 1;
    while(n) {
        if(n & 1) {        // n % 2 == 1
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        n >>= 1;
    }
    return res;
}

int main()
{
    int n;

    cin >> n;

    cout << powermod(n, n, 10) << endl;

    return 0;
}





原文地址:https://www.cnblogs.com/tigerisland/p/7563742.html