HDU1061-Rightmost Digit(高速功率模)

主题链接


题意:求n^n的个位数的值。

思路:高速幂求值

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

typedef __int64 ll;
//typedef long long ll;

const int MOD = 1000000000;

ll n;

ll pow_mod(ll k) {
    if (k == 1)
        return n % MOD;
    ll a = pow_mod(k / 2);
    ll ans = a * a % MOD; 
    if (k % 2 == 1)
        ans = ans * n % MOD;
    return ans;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {               
        scanf("%I64d", &n);
        ll ans = pow_mod(n);
        while (ans > 10) {
            ans %= 10; 
        }
        printf("%I64d
", ans);
    }
    return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4722353.html