杭电 1061 Rightmost Digit计算N^N次方的最后一位
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次方的最后一位的更多相关文章
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- hdoj 1061 Rightmost Digit【快速幂求模】
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDOJ 1061 Rightmost Digit(循环问题)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- HDOJ 1061 Rightmost Digit
找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 快速幂 HDU 1061 Rightmost Digit *
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1061 Rightmost Digit解决问题的方法
求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...
- hdu 1061 Rightmost Digit
解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #in ...
- 【HDOJ】1061 Rightmost Digit
这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...
随机推荐
- 图文并茂 RAID 技术全解 – RAID0、RAID1、RAID5、RAID10
RAID 技术相信大家都有接触过,尤其是服务器运维人员,RAID 概念很多,有时候会概念混淆.这篇文章为网络转载,写得相当不错,它对 RAID 技术的概念特征.基本原理.关键技术.各种等级和发展现状进 ...
- SqlServer-RBAC五表权限
这只是一个精简的SqlServer-RBAC五表权限,根据自身需求修改即可 --创建数据库-权限CREATE DATABASE RBACGOUSE RBACGO --1.权限表CREATE TABLE ...
- java 一个实例
this 代替
- Shell编程基础知识(一)
一.基本的运行Linux程序的3种方法: (1) 使文件具有可执行权限,直接运行文件.eg: chmod a+x testfile.sh ./testfile.sh (2) 直接调用命令解释器来 ...
- 【算法】LeetCode算法题-Longest Common Prefix
这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...
- June 12. 2018 Week 24th. Tuesday
Just be yourself because you are unique and you will shine. 每个人都是独一无二的,做好你自己,你也能够光芒四射. From What a G ...
- VsCode放大缩小
如图:vscode界面突然缩小了 通过快捷键 ctrl+shift+ + 放大 ctrl+shift+ - 缩小
- Ant Design Pro的dva-loading
loading为dva的插件,全局可用,它里面维护了一些布尔值,用于控制loading动画效果的显示与隐藏,通过@connect()来注入使用 官网介绍如下: https://dvajs.com/ 在 ...
- MySQL高级知识(四)——Explain
前言:explain(执行计划),使用explain关键字可以模拟优化器执行sql查询语句,从而知道MySQL是如何处理sql语句.explain主要用于分析查询语句或表结构的性能瓶颈. 注:本系列随 ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...