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. Python中处理日期时间库的使用方法(转载)

    <本文来自公众号“大邓带你玩python”,转载> 用百分之20时间,学会解决百分之80的问题. 常用的库有time.datetime.其中datetime库是对time库的封装,所以使用 ...

  2. Dock

    搭建本地 Registry - 每天5分钟玩转 Docker 容器技术(20) 小结: dock 版本号 分为 3位,比如1.1.2 就分为1, 1.1,1.1,2 这个几个版本 这种 tag 方案使 ...

  3. DB buffer bussy wait 分析一例

    ####sample 1: DB层分析OI DB层分析OI的信息如下: 1. 异常时间段,  Logical reads:/ Physical reads/ Physical write  指标都低于 ...

  4. 413 Arithmetic Slices 等差数列划分

    如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列.例如,以下数列为等差数列:1, 3, 5, 7, 97, 7, 7, 73, -1, -5, -9以下数列不是等差数列. ...

  5. Atcoder B - Boxes 玄学 + 数学

    http://agc010.contest.atcoder.jp/tasks/agc010_b 预处理出每两个相邻的数的差值,那么首先知道是一共取了sum / ((1 + n) * n / 2)次,因 ...

  6. sdut1642Simple Arithmetics(模拟)

    链接 发个长长的模拟 这题要注意的地方挺多 -的个数 以及对齐的情况 全都注意好了 大数的加减乘就可以了 #include <iostream> #include<cstdio> ...

  7. PHP + ORACLE 远程连接数据库环境配置

    在ORACLE官网下载instantclient_11_2,放在D盘 把instantclient_11_2目录下的所有dll文件复制到C:\Windows\SysWOW64   和  D:\phpS ...

  8. 使用一个CSS Class去给标签定义Style

    使用一个CSS Class去给标签定义Style 类是可重用的样式,可以添加到HTML元素. 下面是一个CSS类声明的例子: <style>   .blue-text {     colo ...

  9. Java集合框架源码(四)——Vector

    第1部分 Vector介绍 Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这 ...

  10. WebSocket 的一些简单页面推送使用

    因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...