杭电 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() ...
随机推荐
- mysql练习----Using Null
teacher id dept name phone mobile 101 1 Shrivell 2753 07986 555 1234 102 1 Throd 2754 07122 555 1920 ...
- CentOS7中安装MySQL5.7
安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...
- 在CentOS上配置SAMBA共享目录(转载)
在CentOS上配置SAMBA共享目录 From: https://blog.csdn.net/qiumei1101381170/article/details/53265341 2016年11月21 ...
- nmap参数原理抓包分析
nmap参数原理抓包分析 实验环境: Nmap7.70 实验步骤: 1.主机发现 2.端口扫描 3.服务版本探测 一.主机发现 主机发现,如果主机活跃,扫描1000个常用的tcp端口 1.Nmap i ...
- 【RHEL7.0】软件包管理
1.常用的RPM软件包命令 安装软件的命令格式 rpm –ivh filename.rpm 升级软件的命令格式 rpm –Uvh filename.rpm 卸载软件的命令格式 rpm –e fi ...
- JavaSE: SuppressWarnings[转]
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...
- [福大软工] Z班 第6次成绩排行榜
作业要求 http://www.cnblogs.com/easteast/p/7668890.html 作业评分 本次作业从引言(5 ') . 用户场景(15 ').类图(10 ').界面原型(15 ...
- 使用Intellij IDEA将web项目导出为war包
前言:IDEA导出war包的方式与MyEclipse有一点不同,使笔者在使用的时候有点困惑,在网上查阅相关资料的时候,发现其描述十分不清晰,于是就有了这篇随笔的诞生.话不多说,直接进入正题. 注:ID ...
- Java学习笔记(二)——类和对象
[1]类是模子,确定对象将会拥有的特征(属性)和行为(方法). [2]类的特点:类是对象的类型: 具有相同属性和方法的一组对象的集合. [3]属性:对象具有的各种特征(每个对象的每个属性都拥有特定值) ...
- 第5章 Java中的锁
5.1 Lock接口 并发编程安全性需要使用到锁,synchronized是一种隐式的获得与释放锁的关键字,除此之外还有Lock接口及其实现类,该接口及实现类提供了显示获取和释放锁的方式. 除了上述编 ...