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

快速幂+取余
#include<cmath>
#include<cstdio> 
#include<iostream> 
using namespace std;
const int mod=10;
int q_pow(int x) {
    int ans=1,tmp=x;
    while(x) {
        if(x&1)ans=(ans%mod)*(tmp%mod)%mod;
        tmp=(tmp%mod)*(tmp%mod)%mod;
        x>>=1;
    }
    return ans;
}
int main () {
    int n;
    scanf("%d",&n);
    //n%=10;
    printf("%d
",q_pow(n));
    return 0;
}
原文地址:https://www.cnblogs.com/sssy/p/7732573.html