HDU 1061 Rightmost Digit --- 快速幂取模
题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果
解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的,
因为会超出数据范围,即使是long long也无法存储。
因此需要利用 (a*b)%c = (a%c)*(b%c)%c,一直乘下去,即 (a^n)%c = ((a%c)^n)%c;
即每次都对结果取模一次
此外,此题直接使用朴素的O(n)算法会超时,因此需要优化时间复杂度:
一是利用分治法的思想,先算出t = a^(n/2),若n为奇数,则返回t*t*a,偶数则返回t*t;
二是使用通过循环实现快速幂取模(其实二者实质上是相同的)。
1.递归解法
/* HDU 1061 Rightmost Digit --- 快速幂取模 */
#include <cstdio> /*
@function: 计算n^n%10
@param: n为待计算的数
@return: 返回n^n%10的结果
@explain: 利用分治策略以及同余定理实现快速幂取模
*/
int pow_mod(int a, int n){
if (n == ){
return ;
}
int x = pow_mod(a, n / ); //x = a^(n/2)
long long ans = (long long)x * x % ;
if (n & ){
//若n为奇数
ans = ans * a % ;
}
return (int)ans;
} int main()
{
int t, n;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
printf("%d\n", pow_mod(n, n));
} return ;
}
2.快速幂取模
/* HDU 1061 Rightmost Digit --- 快速幂取模 */
#include <cstdio> /* 快速幂取模 */
int pow_mod(int a, int n){
int ans = ;
int t = a % ;
while (n){
if (n & ){
//n为奇数
ans = ans * t % ;
}
n /= ; //相当于将n拆成相应的二进制
t = t * t % ;
}
return ans; } int main()
{
int t, n;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
printf("%d\n", pow_mod(n, n));
} return ;
}
HDU 1061 Rightmost Digit --- 快速幂取模的更多相关文章
- 题解报告: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-规律题 or 快速幂取模
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- 杭电 2817 A sequence of numbers【快速幂取模】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数 ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- POJ3641-Pseudoprime numbers(快速幂取模)
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...
- 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...
随机推荐
- poj 1742 Coins (动态规划,背包问题)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32977 Accepted: 11208 Descripti ...
- 统计一段文字中出现频率最高的10个单词(c语言)
注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...
- 操作系统:cpu调度 6-25
1. 进程选择 1小时和1分钟? 进程优先1分钟,再执行1小时. 时间短的进程先执行,执行顺序也有关. 2. 遇到io操作,执行的进程先让出cpu,切换其他进程. 3.进程先来先服务,进程调度策略: ...
- Oracle 中批量导入大量测试数据的方法
执行如下批处理命令: declare maxrecords constant int := 100000; i int := 1;begin for i in 1 .. 20000 loop ...
- operation not possible due to RF-kill
使用mdk3时出现这个问题operation not possible due to RF-kill 就是输入第一条命令 后出现 operation not possible due to RF-ki ...
- Ubuntu 14.10 下安装java反编译工具 jd-gui
系统环境,Ubuntu 14.10 ,64位 1 下载JD-GUI,网址http://221.3.153.126/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/jd.benow.ca/ ...
- activeMQ设置admin的用户名和密码
ActiveMQ使用的是jetty服务器, 打开conf/jetty.xml文件,找到 <bean id="securityConstraint" class="o ...
- php大力力 [019节]php分页类的学习
2015-08-26 php大力力019.php分页类的学习 [2014]兄弟连高洛峰 PHP教程14.2.1 分页需求分析 14:18 [2014]兄弟连高洛峰 PHP教程14.2.2 分页类中分页 ...
- 用python做些有意思的事——分析QQ聊天记录——私人订制
之前,写了这篇文章,用python提取全部群成员的发言时间,并简单做了下分析.先补充一下,针对特定单个群成员(这里以 小小白 为例)消息记录的获取. 代码比较简单,主要是正则表达式的书写.(附: ...
- Why does my ListView scroll to the top when navigating backwards?
I’ve seen a few people asking this question. They have a page which contains a ListView and when an ...