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). ...
随机推荐
- 启动BPM的5个步骤
在大部分业务中,我们通常认为:一个主要的业务流程管理项目从设计时间开始会比较好.我们知道很多方式来提高效率,增加生产力以及简化我们员工的工 作 - 这正是业务流程管理所做的.不幸的是,不管我们意图多好 ...
- Portlet和servlet的区别
相同之处 l 都是java技术开发的web组件 l 都是由特定的容器在管理 l 都可以动态产生各种内容 l 生命周期都是由容器管理 l 和客户端的交互通过request/response机制 不同之处 ...
- ie浏览器兼容性快速处理小招
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- matlab图像剪裁命令imcrop()
调用格式: I2=imcrop(I,RECT): X2=imcrop(X,MAP,RECT): RGB2=imcrop(RGB,RECT): 其中,I.X.RGB分别对应灰度图像.索引图像.RGB图像 ...
- php中常用的运算符
运算符 运算符是告诉PHP做相关运算的标识符号. PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. 1.变量名记得加“$” 符: 2. ...
- CGAffineTransformMakeTranslation和CGAffineTransformTranslate
分类: ios基础2013-01-06 22:05 15513人阅读 评论(2) 收藏 举报 1.CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照 ...
- (转)js的左右滑动触屏事件
原文:http://blog.sina.com.cn/s/blog_6a0a183f0100zsfk.html (2012-01-20 08:55:53) 转载▼ 标签: 移动设备 触屏事件 杂谈 分 ...
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla ...
- application:didFinishLaunchingWithOptions:详解
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- Win8远程[你的凭据不工作]的解决办法
用户名前加你的计算机名称就可以了....win8也会有这样的问题,有点匪夷所思了......