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. 服务器体系(SMP, NUMA, MPP)与共享存储器架构(UMA和NUMA)

    1. 3种系统架构与2种存储器共享方式 1.1 架构概述 从系统架构来看,目前的商用服务器大体可以分为三类 对称多处理器结构(SMP:Symmetric Multi-Processor) 非一致存储访 ...

  2. [Hive_add_1] Hive 与 MR 的对应关系

  3. Linux下键盘值 对应input_evnet的code值。

    最近做了一个linux下面的模拟鼠标和键盘的app,但不是很清楚字符对应的键值:查找内核源码,在kernel/include/uapi/linux/input.h文件中找到: 下面给出普通键盘上面对应 ...

  4. 回调函数的原理及PHP实例

    背景:在最近的一个开发项目中,用户要先调用服务才能开始进行一系列的查询活动,想了好久,经同事提醒, 用回调函数即可解决该问题.在这里,对PHP下回调函数的原理及实现分别做一下讲解. 1 什么是回调 软 ...

  5. Git&GitHub

    Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.  Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软 ...

  6. 十大经典排序算法的python实现

    十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序.包括:冒泡排序.选择排序.归并排序.快速 ...

  7. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  8. February 26th, 2018 Week 9th Monday

    A good beginning is half done. 良好的开端是成功的一半. We can't finish anything if we don't start, sometimes ge ...

  9. formbuild拖拽表单设计器

        formbuild拖拽表单设计器 表单设计器适用于OA系统.问卷调查系统.考试系统等系统,具体使用请前至官网API请点击 formbuild拖拽表单设计器     formbuild迭代几个功 ...

  10. IT部门不应该是一个后勤部门

    管理上最大的问题在于不重视预算与核算的管理.从管理层到员工,很少有经营的念头,只是一味地埋头做事.西方企业总结了当今几百年的经营理念,最终把企业一切活动的评价都归结到唯一的.可度量的标准上:钱来度量. ...