首页 > 其他 > 详细

poj1061

时间:2017-01-20 20:23:12      阅读:168      评论:0      收藏:0      [点我收藏+]

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

#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;
}

 

poj1061

原文:http://www.cnblogs.com/lulichuan/p/6323652.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!