hdu Rightmost Digit

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061

水题快速幂,求余进行最后一个取尾操作就可以,坑点不大,

直接上代码:

Talk is cheap. Show me the code.

#include<bits/stdc++.h>
using namespace std;
long long t;
long long fastpow(long long a,long long b)
{
    long long base=a;
    long long res=1;
    while(b)
    {
        if(b&1)
        res=(res*base)%10;
        base=(base*base)%10;
        b>>=1;
    }
    return res;
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long ans=0;
        ans=fastpow(n,n)%10;
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2022-05-03 19:25  江上舟摇  阅读(8)  评论(0编辑  收藏  举报