杭电 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() ...
随机推荐
- JSF中run项目时候Tomcat8启动不了的一种方法
把另一个博客内容迁移到这 我的问题是Tomcat是可以启动的 但是run那个jsp的时候 七月 10, 2016 3:14:54 下午 org.apache.tomcat.util.digester. ...
- [20190214]11g Query Result Cache RC Latches.txt
[20190214]11g Query Result Cache RC Latches.txt --//昨天我重复链接http://www.pythian.com/blog/oracle-11g-qu ...
- 通过http上下文判断是否是Ajax请求
using System; namespace System.Web.Mvc { /// <summary>Represents a class that extends the < ...
- oracle USING 用法
提问 using(xx)中可以接多个列吗? using(xx)中的列可以接表名或别名吗? 在使用using的语句中,select * 可以使用吗? 如果表有别名t,select t.* from ta ...
- kafka_2.11-2.0.0_安装部署
参考博文:kafka 配置文件参数详解 参考博文:Kafka[第一篇]Kafka集群搭建 参考博文:如何为Kafka集群选择合适的Partitions数量 参考博文:Kafka Server.prop ...
- Git版本管理工具常用命令说明
Git常用命令 $ touch README.md 创建一个README.md文件 $ git init 创建本地仓库(repository),将会在文件夹下创建一个 .git 文件夹,.git 文 ...
- python while and for
一.while循环 1.格式: while 条件: while循环体 else: 循环正常跳出执行的语句 2.实例: index= : : break #直接跳出while ,不会执行else els ...
- 【大数据技术】Hadoop三大组件架构原理(HDFS-YARN-MapReduce)
目前,Hadoop还只是数据仓库产品的一个补充,和数据仓库一起构建混搭架构为上层应用联合提供服务. Hadoop集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起. ...
- spring boot 的maven设置阿里云仓库
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...
- node基础—global对象(全局对象)
global对象的__filename属性和__dirname属性 __filename属性:返回当前执行的文件的文件路径,该路径是经过解析后的绝对路径,在模块中,该路径是模块文件的路径,此属性并非全 ...