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 <iostream>
using namespace std;
int a[]={,,,,,,,,,,,,,,,,,,,,};
int main()
{
int b,n;
cin>>b;
while(b--)
{
cin>>n;
cout<<a[n%]<<endl;
}
return ;
}

方法二:

 #include<stdio.h>
int my_power(int m, int n); // 求m的n次方的尾数
int main()
{
int cases, n;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &n);
printf("%d\n", my_power(n, n));
} return ;
} int my_power(int m, int n)
{
m = m%;
if(n == )
return m;
if(n% == )
return ( my_power(m*m, n/) ) % ;
else
return ( my_power(m*m, n/)*m ) % ;
}

快速幂的时间复杂度是O(logn),n = 10亿时,大约32次递归调用就能出结果,效率极大的提高了

Rightmost Digit(快速幂)的更多相关文章

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

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

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

  3. hdu1061Rightmost Digit(快速幂取余)

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

  4. HDU-1061-Rightmost Digit(快速幂)

    快速幂(本代码中的^表示次幂不是异或) Accepted 1061 0MS 1368K 679 B G++ #include "bits/stdc++.h" using names ...

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

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

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

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

  7. 快速幂 HDU 1061 Rightmost Digit *

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

  8. HDU 1061 Rightmost Digit (快速幂取模)

    题意:给定一个数,求n^n的个位数. 析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的, 找一下就OK了. 代码如下: #include <iostrea ...

  9. HDU 1061 Rightmost Digit( 快速幂水 )

    链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /********************************************************************** ...

随机推荐

  1. C. Captain Marmot (Codeforces Round #271)

    C. Captain Marmot time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. Python在信号与系统(1)——Hilbert兑换,Hilbert在国家统计局的包络检测应用,FIR_LPF滤波器设计,格鲁吉亚也迫使高FM(PM)调制

    谢谢董老师,董老师是个好老师. 心情久久不能平静,主要是高频这门课的分析方法实在是让我难以理解,公式也背只是,还是放放吧. 近期厌恶了Matlab臃肿的体积和频繁的读写对我的Mac的损害,所以学习了一 ...

  3. 系统负载测试工具-LoadRunner

    LoadRunner的主要作用是对系统压力测试进行分析 与之相类似的工具是:badboy:录制脚本工具+jmeter:分析结果工具

  4. 轻量级的内部测试过程r \\ u0026研发团队

    对于一个r \\ u0026研发团队的目的,标准化的工作流程资产不可或缺的一部分,特别是对于初创的r \\ u0026研发团队方面.很多r \\ u0026研发管理是不够完整.如何理解的研发团队中的各 ...

  5. jq实现图像旋转木马:轮焦点+关于控制+自己主动旋转木马

    资源:http://www.ido321.com/862.html html代码: 1: <!DOCTYPE html> 2: <html lang="en"&g ...

  6. cocos2d-x v3.2环境配置(现在3.x版本号可以配置该)

     这里介绍的是Windows环境下的cocos2d-x配置. 前提: •Windows 7+ •cocos2d-x v3.2版本号(能够在http://www.cocos2d-x.org/down ...

  7. POJ 1699 Best Sequence (DFS+预处理)

    意甲冠军:看图片是晶莹剔透的,正确的, N连接到第一序列(同样的序列部分).总序列获得最短. 主题链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾 ...

  8. SharePoint 2013 创建一个搜索中心和搜索设置

    这篇文章不是太多深奥的东西,只是一个简单的搜索配置,假设你已经有了,请跳过这篇文章. 行,输入信息,大家都知道,搜索SharePoint一个主要特征.下列,我们在搜索中心创建个人资料. 1.创建Sea ...

  9. SDL2来源分析3:渲染(SDL_Renderer)

    ===================================================== SDL源代码分析系列文章上市: SDL2源码分析1:初始化(SDL_Init()) SDL2 ...

  10. 第二章_session管理

    2.1 URL重写 URL重写是Session追踪技术.须要将一个或多个token做为一个查询字符串加入到一个URL中. Token的格式通常是键=值. Url?key-1=value-1&k ...