Rightmost Digit

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

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.

题意:给出n,要求算出n^n的最右边一位。

题解:普通方法进行幂运算再进行取余会超时,需要用到快速幂

 #include<bits/stdc++.h>
using namespace std;
int mod_exp(int a, int b, int c) //快速幂取余a^b%c
{
int res, t;
res = % c;
t = a % c;
while (b)
{
if (b & )
{
res = res * t % c;
}
t = t * t % c;
b >>= ;
}
return res;
}
int main() {
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",mod_exp(n,n,)%);
}
}
return ;
}

hdu1061Rightmost Digit(快速幂取余)的更多相关文章

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

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

  2. 洛谷 P1226 【模板】快速幂||取余运算

    题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...

  3. LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

    链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...

  4. 洛谷P1226 【模板】快速幂||取余运算

    题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 S1: ...

  5. LuoguP1226 【模板】快速幂||取余运算

    题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...

  6. [每日一题2020.06.15]P1226 【模板】快速幂取余运算

    我是题目 快速幂就是快速求 \(a^b\)的一种算法 快速幂 思想 : 比如我要求 \(6^9\) 首先将幂转化为二进制形式 : \[6^9 = 6^{1001} \tag{1} \] 可以得到 : ...

  7. 洛谷 P1226 【模板】快速幂||取余运算 题解

    Analysis 快速幂模板,注意在最后输出时也要取模. 快速幂模板 inline ll ksm(ll x,ll y) { ll ans=; ) { ) { ans*=x; ans%=k; } x*= ...

  8. 【模板】快速幂&取余运算

    输入\(b\),\(p\),\(k\)的值,求\(b^p mod k\)的值.其中\(b\),\(p\),\(k^2\)为长整型数. 1.普通做法 \(print\) \(pow(b,p)\)\(mo ...

  9. 【洛谷P1226 【模板】快速幂||取余运算】

    题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 作为初 ...

随机推荐

  1. js 关联着数组中追加元素

    var arr_data = new Array(); for ( var i in data.data ){ arr_data.push(arr_data[i] = data.data[ i ] ) ...

  2. 剑指offer5 从尾到头打印链表

    错误代码: class Solution { public: vector<int> printListFromTailToHead(ListNode* head){ vector< ...

  3. 在网页中使用Markdown

    在网站中使用markdown有两种方式,一种是通过后端(php等)把markdown语法文本转换为html代码,输出到浏览器:另一种是通过javascript代码直接在浏览器中转换. 我在这里使用的是 ...

  4. CSS布局方面的一些小总结

    1. display属性 display是CSS布局的第一站,它控制一个元素以什么“身份”出现在页面布局当中.它的值有很多个,常用的有block,inline,inline-block,table,n ...

  5. POJ 3122 Pie(二分+贪心)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22684   Accepted: 7121   Special Ju ...

  6. HDU 2036 改革春风吹满地 (求多边形面积)

    传送门: 改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. Knowledge Point 20180303 详解main函数

    学习Java的朋友想来都是从HelloWorld学起的,那么想来都对main函数不陌生了,但是main函数究竟是怎么回事呢?main函数中的参数是做什么的呢?main函数为什么能作为程序的入口呢?可不 ...

  8. Oracle中转义下划线

    原意是查询出所有的月粒度模型,但是在oracle中,下划线也代表匹配单一任何字符,导致15分钟粒度的模型也被查询出来,在此,需要对下划线做转义,使其只表示下划线的含义,可以使用ESCAPE()函数. ...

  9. Oracle GoldenGate同步服务归档空间维护

    ORA-00257: archiver error. Connect internal only, until freed 查看归档日志序列 SQL> archive log list; Aut ...

  10. Tesseract-OCR 自动生成识别库的批处理

    用Tesseract-OCR做识别库的时候,生成字典非常麻烦,就写了一个批处理,用来生成字典还是蛮方便的,希望大家有用,该批处理已经自动生成font_properties文件,各位无需手动创建 下载地 ...