Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55522    Accepted Submission(s): 20987

Problem Description
Given a positive integer N, you should output the most right digit of N^N.
 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
 
Output
For each test case, you should output the rightmost digit of N^N.
 
Sample Input
2
3
4
 
Sample Output
7
6

Hint

In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

 
 
第一个,找规律,代码:
#include<stdio.h>
typedef long long ll;
int main(){
int a[][]={{},{},{,,,},{,,,},{,},{},{},{,,,},{,,,},{,}};
ll m;
int n,i,h;
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%lld",&m);
h=m%;
if(h==||h==||h==||h==)
printf("%d",h);
else if(h==||h==)
printf("%d",a[h][m%]);
else
printf("%d",a[h][m%]);
printf("\n");
}
}
return ;
}

第二个,快速幂取模,代码:

#include<stdio.h>
typedef long long ll;
ll mod=1e5;
ll pow(ll a,ll b){
ll ans=;while(b!=){
if(b%==)
ans=ans*a%mod;
a=a*a%mod;
b=b/;
}
return ans;
}
int main(){
ll n,m,ans;
while(~scanf("%lld",&n)){
while(n--){
scanf("%lld",&m);
ans=pow(m,m)%;
printf("%lld\n",ans);
}
}
return ;
}

HDU 1061.Rightmost Digit-规律题 or 快速幂取模的更多相关文章

  1. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  2. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  3. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  4. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  5. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  6. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  7. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

  8. Powmod快速幂取模

    快速幂取模算法详解 1.大数模幂运算的缺陷: 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算 ...

  9. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

随机推荐

  1. [剑指Offer] 22.从上往下打印二叉树

    [思路]广度优先遍历,队列实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { qu ...

  2. [CF1041E]Tree Reconstruction

    题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...

  3. 【BZOJ 1485】[HNOI2009]有趣的数列 卡特兰数

    这个题我是冲着卡特兰数来的所以就没有想到什么dp,当然也没有想到用卡特兰数的原因........... 你只要求出前几项就会发现是个卡特兰数,为什么呢:我们选择地时候要选择奇数位和偶数位,相邻(一对里 ...

  4. ext radiogroup如何取值和设值

    var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...

  5. springMvc--请求的跳转和传值

    springMvc--请求的跳转和传值 目录 forword跳转页面的三种方式 1.使用serlvet 2.使用Model对象 3.使用ModelAndView redirect跳转到页面 使用ser ...

  6. [ZJOI2005]午餐 (DP)

    [ZJOI2005]午餐 题目描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口 ...

  7. Java 处理 XML 的三种主流技术及介绍

    Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...

  8. Install the AWS Command Line Interface on Linux

    Install the AWS Command Line Interface on Linux You can install the AWS Command Line Interface and i ...

  9. barba 页面渲染

    a.css html, body { padding:; margin: 0 } ol.menu { width: 100%; text-align: left; padding: 0 !import ...

  10. centos yum 安装 mysql

      centos7下使用yum安装mysql 时间:2015-03-07 21:26:20      阅读:87445      评论:0      收藏:1      [点我收藏+] 标签: Cen ...