Rightmost Digit (求n^n最后一位)
Description
Input
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
Sample Input
3
4
Sample Output
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%100)^(n%100)的最后一位。
#include<cstdio>
int main()
{
__int64 t,l,b;
scanf("%I64d",&t);
while(t--)
{
__int64 n;
scanf("%I64d",&n);
l=n%;
b=l;
for(__int64 i=;i<l;i++)
{
b=b*l;
b%=;
}
b%=;
printf("%I64d\n",b);
}
}
快速幂
快速幂求n^n;
1 int f1(int a,int b)
2 {
3 int t=1;
4 while(b)
5 {
6 if(b % 2 != 0)
7 {
8 t*=a;
9 b--;
10 }
11 a*=a;
12 b/=2;
13 }
14 return t;
15 }
快速幂求n^n后y位;
1 int f2(int a,int b)
2 {
3 int t=1;
4 while(b)
5 {
6 if(b % 2 != 0)
7 {
8 t=(t*a)%x; //x控制要求的位数
9 b--;
10 }
11 a=(a*a)%x;
12 b/=2;
13 }
14 return t;
15 }
代码
#include<cstdio>
__int64 f(__int64 a)
{
__int64 b=a;
__int64 t=;
while(b)
{
if(b%!=)
{
t=(t*a)%;
b--;
}
a=a*a%;
b/=;
}
return t;
}
int main()
{
__int64 t,a;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d",&a);
printf("%I64d\n",f(a));
}
}
Rightmost Digit (求n^n最后一位)的更多相关文章
- Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏
C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Rightmost Digit(最后一位数字)
Description Given a positive integer N, you should output the most right digit of N^N. Input The ...
- <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字
1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...
- 杭电 1061 Rightmost Digit计算N^N次方的最后一位
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- hdoj 1061 Rightmost Digit【快速幂求模】
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU1061 - Rightmost Digit
Given a positive integer N, you should output the most right digit of N^N. Input The input contains ...
- Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- Rightmost Digit(快速幂)
Description Given a positive integer N, you should output the most right digit of N^N. ...
随机推荐
- 依赖注入(二)Autofac简单使用
Autofac简单使用 源码下载传上源码,终于学会传文件了. 首先 还是那句话:“不要信我,否则你死得很惨!”. C#常见的依赖注入容器 IoC in .NET part 1: Autofac IoC ...
- poj 1664 放苹果 递归
题目链接: http://poj.org/problem?id=1664 题目描述: 有n个苹果,m个盒子,盒子和苹果都没有顺序,盒子可以为空,问:有多少种放置方式? 解题思路: 当前有n个苹果,m个 ...
- Android 线程池系列教程(1)目录
Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...
- 16-2 基于localStorage或sessionStorage的计数器
localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. <!doctype html> <html> ...
- sdut2784&cf 126b Good Luck!(next数组)
链接 next数组的巧妙应用 学弟出给学弟的学弟的题.. 求最长的 是前缀也是后缀同时也是中缀的串 next的数组求的就是最长的前后缀 但是却不能求得中缀 所以这里 就把尾部去掉之后再求 这样就可 ...
- Uncaught TypeError: Cannot set property 'f7View' of undefined 错误原因
// 添加视图var mainView = myApp.addView('.view-main', { // 因为我们要用动态的导航栏,我们需要使它的这一观点: dynamicNavbar: true ...
- (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记
1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...
- php高效率对一维数组进行去重
$input = array("a" => "green", "red", "b" => "gre ...
- Sql Server 2012 事务复制遇到的问题及解决方式
1.订阅服务器提示:作业失败.无法确定所有者 WIN-01Q6JB46CHV\Administrator(拥有作业XXX)是否有服务器访问权限(原因:无法获取有关 Windows NT 组/用户'WI ...
- call和apply和bind的区别
在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. JavaScript 的一大 ...