HDU_1061:Rightmost Digit
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
3
4
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.
#include<stdio.h>
int main(void)
{
int cases, n, copy_n, result;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &n);
copy_n = n;
n = n%;
result = ;
while(copy_n--)
{
result = (result*n)%;
}
printf("%d\n", result);
} return ;
}
下面,快速幂一来就AC了:
#include<stdio.h>
int my_power(int m, int n); // 求m的n次方的尾数
int main(void)
{
int cases, n;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &n);
printf("%d\n", my_power(n, n));
} return ;
} int my_power(int m, int n)
{
m = m%;
if(n == )
return m;
if(n% == )
return ( my_power(m*m, n/) ) % ;
else
return ( my_power(m*m, n/)*m ) % ;
}
可以看到,快速幂的时间复杂度是O(logn),n = 10亿时,大约32次递归调用就能出结果,效率极大的提高了。
HDU_1061:Rightmost Digit的更多相关文章
- Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏
C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Rightmost Digit(快速幂)
Description Given a positive integer N, you should output the most right digit of N^N. ...
- <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字
1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...
- 杭电 1061 Rightmost Digit计算N^N次方的最后一位
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 ...
随机推荐
- LINUX对于未安装的软件包的查看
查看的前提是您有一个.rpm 的文件,也就是说对既有软件file.rpm的查看等: 1.查看一个软件包的用途.版本等信息: 语法: rpm -qpi file.rpm 举例: [root@localh ...
- shell 第一篇
1. 查看当前linux 支持的shell 类型 [root@nfs01 ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/dash / ...
- NoClassDefFoundError: Could not initialize class org.apache.cxf.jaxrs.provider.ProviderFactory org.springframework.aop.support.AopUtils.isCglibProxyClass
报错: 2018-05-03 10:35:20 377 ERROR org.apache.juli.logging.DirectJDKLog.log:181 - Servlet.service() f ...
- Sql 竖表转横表
) set @sql='select t3.BID,t5.UnitName,Sort,UnitTypeSort' select @sql=@sql+' , max(case t4.id when '' ...
- bzoj 3743 [Coci2015]Kamp——树形dp+换根
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3743 树形dp+换根. “从根出发又回到根” 减去 “mx ” . 注意dfsx里真的要改那 ...
- 手机前端开发调试利器-vConsole
最近因为做抽奖页面,在android上可以使用手机连上电脑后用chrome浏览器chrome://inspect进行页面探测,但是ios中的页面就不能这样探测 在网上搜索后发现此插件,大大解决了问题 ...
- JS为什么是单线程的?
JavaScript语言最大的特点就是单线程.它是浏览器的脚本语言.在同一时间只能做一件事.用于操作DOM.如果JS是多线程的,当我在给一个DOM添加内容时,又删除了这个DOM,那么JS该怎么做. 关 ...
- Linux & CentOS & RHEL
1.修改centos7的系统编码:https://blog.csdn.net/violet_echo_0908/article/details/58063555 2.windows 环境下使用ultr ...
- matplotlib无法显示中文
import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['KaiTi']mpl.rcParams['font.serif'] = ['K ...
- mysql连接出现Unknown system variable 'tx_isolation'异常
出现这个异常,是因为mysql-connector-java.jar的版本太低,数据库的版本太高,不匹配导致的. 因此将mysql-connector-java升级到最新版本就解决了问题. 最新的三个 ...