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. js操作string它substr方法

    substr 方法 从指定的指定起始位置的长度返回一个子. stringvar.substr(start [, length ]) 參数 stringvar 必选项. 要提取子字符串的字符串文字或 S ...

  2. Windows Azure应用系列:微软的云部署VPN

    本文介绍如何使用OpenVPN微软云计算server既定VPN维修. 过程,如下面: 1.新建Linux或者Ubuntu虚拟机.并设置port.(本文将建立Ubuntu作为演示) 2.利用PuTTY登 ...

  3. Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service

    原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...

  4. POJ 2155 D区段树

    POJ 2155  D区段树 思考:D区段树是每个节点设置一个段树树. 刚開始由于题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂.后面自己把图画出来了才理解. 由于仅 ...

  5. sort和qsort排序

    qsort(数组名,数组长度,数组中每个元素大小,compare); compare函数的写法决定了排序是升序还是降序.需要#include<stdlib.h> 例如: int compa ...

  6. C#的WebBrowser控制浏览

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 【原版的】PHP技术成长规划过程中猿人

    PHP程序猿的技术成长规划 作者:黑夜路人(2014/10/15) 依照了解的非常多PHP/LNMP程序猿的发展轨迹.结合个人经验体会,抽象出非常多程序猿对未来的迷漫,特别对技术学习的盲目和慌乱.简单 ...

  8. css两种动态显示星星等级的比较(一星、两星、三星、四星、五星)

    原文:css两种动态显示星星等级的比较(一星.两星.三星.四星.五星) 以下是显示后的图片,相信在很多网站上都能看到这种效果,目前我知道两种实现方式 1.background-position加上一张 ...

  9. Win7 补丁装不上怎么办?

    检查系统盘文件系统是否出错: 打开管理员命令行程序(Win+R,输入 cmd 确认) 如果系统盘是 C 盘,命令行窗口输入 chkdsk C: /f 对提示输入’y’ 重启等待 系统文件检查: 打开管 ...

  10. 2C 产品的本质是人性,2B 产品的背后是业务(转)

    本文作者李源是 BLUES 原来做 YY 语音客户端产品时候的同事,原来针对 YY 语音的游戏用户做 2C 的 PC 客户端产品和 APP,后来到某品牌手机做 2B 的后台系统.以下文章,是作者经历了 ...