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%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最后一位)的更多相关文章

  1. 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 ...

  2. Rightmost Digit(最后一位数字)

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

  3. <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字

    1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...

  4. 杭电 1061 Rightmost Digit计算N^N次方的最后一位

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

  5. hdoj 1061 Rightmost Digit【快速幂求模】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. HDU1061 - Rightmost Digit

    Given a positive integer N, you should output the most right digit of N^N. Input The input contains ...

  7. Rightmost Digit

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

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

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

  9. Rightmost Digit(快速幂)

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

随机推荐

  1. iOS端样式错位

    在iOS端上点击的时候触发点会在当前元素上方,原因是在最外层使用了fixed定位,换成绝对或相对定位解决问题

  2. VirtualBox搭建1主2从虚拟机

    环境要求 最近在使用VirtualBox搭建一个实验环境,由于公司规定了所有的机器都不能使用固定IP,都必须由DHCP自动获取. 为了不影响公司整理的网络环境,只能把实验用的网络环境限制在使用内部IP ...

  3. 重置iptables

    # reset the default policies in the filter table.iptables -P INPUT ACCEPTiptables -P FORWARD ACCEPTi ...

  4. [POI2009]Tab

    Description 2个n\(\times\)m矩阵,保证同一个矩阵中元素两两不同.问能否通过若干次交换两行或交换两列把第一个矩阵变成第二个. Input 第一行正整数T(1≤T≤10)表示数据组 ...

  5. _bzoj1500 [NOI2005]维修数列【真·Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...

  6. Vasiliy's Multiset CodeForces -706D || 01字典树模板

    就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...

  7. 题解报告:CODE[VS] 1082 线段树练习3(区间修改+区间查询)

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  8. [转]C# 邮箱验证激活

    原文链接 /// <summary> /// 发送邮件 发送激活码 /// </summary> /// <param name="address"& ...

  9. 一个页面通过iframe,获取另一个页面的form

    document.getElementsByTagName("iframe")[0].contentWindow.document.forms[0].submit(); var z ...

  10. [转]C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等

    本文转自:http://www.cnblogs.com/suizhikuo/p/3791799.html 我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Inte ...