• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 1061 Rightmost Digit (快速幂取模)

题意:给定一个数,求n^n的个位数。

析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的,

找一下就OK了。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>

using namespace std;

int pow_mod(int a, int b, int m){
    int ans = 1;
    int k = a % m;
    while(b){
        if(b & 1){
            ans = (ans * k) % m;
            --b;
        }
        b >>= 1;
        k = (k * k) % m;
    }
    return ans;
}

int main(){
    int n, x, T;  cin >> T;
    while(T--){
        scanf("%d", &n);
        printf("%d\n", pow_mod(n, n, 10));
    }

    return 0;
}

 

posted on 2016-06-02 13:22  dwtfukgv  阅读(157)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3