poj1061

一个快速幂取模的题,用快速幂就可以解决了

#include <iostream>
#include <cstdio>
using namespace std;
int mod_exp(int a, int b, int c)        //快速幂取余a^b%c
{
    int res, t;
    res = 1 % c; //a是底数  b是指数;
    t = a % c;
    while (b)
    {
        if (b & 1)
        {
            res = res * t % c;
        }
        t = t * t % c;
        b >>= 1;
    }
    return res;
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        cout << mod_exp(n, n, 10) << endl;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/lulichuan/p/6323652.html