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

  

我写的错误???错误的原因是超时,

//rightmost digit
#include<iostream>
using namespace std;
int main()
{
int n,tmp,m;
cin>>n;
while(n--){
cin>>m;
tmp=;
m%=;
// cout<<"m-==="<<m<<endl;
for(int i=;i<m;i++)
tmp=tmp*m%;
cout<<tmp<<endl;
}
}

正确代码:

#include<iostream>
using namespace std;
int main()
{
int a,ans,n=;
double dval = ;
int count=;
cin>>n;
while(n--)
{
cin>>a;
ans=;
count=a;
a = a%;
while (count)
{
if (count&==)
ans=(ans*a)%;
a=(a*a)%;
count>>=;
}
cout<<ans<<endl;
}
return ;
}

解法二:

读完题首先想到的是大数,但是这大数貌似也太大了,就算能放下,这么多大数乘法铁定超时,考虑优化,写个小程序打表观察能发现这样一个规律,n的次方是有周期性的,且周期为4,这样就好办了,对于给定的N,求N*N的个位数,只需算N的个位数的N%4次方,然后对10取余就是所要求的结果了

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n;
m = n % ;
if (m == )
m = ;
n = n % ;
cout <<int( pow(n, m)) % << endl;
}
return ;
}

PS:

//利用二进制输出 输入的数字
#include<iostream>
using namespace std;
int main()
{
int test,t,n,ans;
while(cin>>n){ test=n;
t=;
ans=;
while(test){
if(test&==) ans+=t;
t*=;
test>>=;
}
cout<<ans<<endl;
}
}

杭电 1061 Rightmost Digit计算N^N次方的最后一位的更多相关文章

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

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

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

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

  3. HDOJ 1061 Rightmost Digit(循环问题)

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

  4. HDOJ 1061 Rightmost Digit

    找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

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

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

  6. 快速幂 HDU 1061 Rightmost Digit *

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

  7. HDU 1061 Rightmost Digit解决问题的方法

    求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...

  8. hdu 1061 Rightmost Digit

    解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #in ...

  9. 【HDOJ】1061 Rightmost Digit

    这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...

随机推荐

  1. 数据库之mysql篇(3)—— mysql创建/修改数据表/操作表数据

    创建数据表:create table 数据表名 1.创建表规范 create table 表名( 列名   数据类型    是否为空   自动排序/默认值  主键/外键/唯一键, 列名   数据类型 ...

  2. IPv6简介

    https://segmentfault.com/a/1190000008794218 IPv6的优点 更大的地址空间 名字叫IPv6,但它的长度并不是64位,而是128位,总的地址空间大约为3.4* ...

  3. Python 入门:基本语法

    对于多数从其他编程语言转入Python的来说,或多或少会有些不习惯.如果沿用其他编程语言的语法来写Python代码,那么碰壁是不可避免的了. 本文是基于我看了两个小时的官方文档(Python 2.7 ...

  4. C# -- 接口 (关键字:interface)

    C#: 接口(关键字:interface) 1.代码(入门举例) class Program { static void Main(string[] args) { Console.WriteLine ...

  5. Linux下完全删除用户

    实验环境:Centos7虚拟机 首先创建一个普通用户gubeiqing. [root@localhost ~]# useradd gubeiqing [root@localhost ~]# passw ...

  6. Java JDK与JRE

    JDK与JRE JDK(Java Development Kit):Java开发工具包.包含JRE中的所有内容,以及用于开发小应用程序和应用程序的编译器和调试器等工具. JRE(Java Runtim ...

  7. Python虚拟环境笔记

    虚拟环境 为什么需要虚拟环境: 到目前位置,我们所有的第三方包安装都是直接通过pip install xx的方式进行安装的,这样安装会将那个包安装到你的系统级的Python环境中.但是这样有一个问题, ...

  8. nginx-redirect配置

    转载一篇非常好的文章,大赞!!!!! http://blog.csdn.net/u010391029/article/details/50395680 nginx的配置文件解读 http://blog ...

  9. leetCode练习1

    代码主要采用C#书写 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你 ...

  10. Linux 创建静态库(.a)和动态库(.so)

    0. 回顾一下 gcc 选项 ============================================== -E : 仅做预处理,例如去注释,宏展开,include 展开等 -S : ...