HDU 1061

  题目大意:给定数字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 --- 快速幂取模的更多相关文章

  1. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  2. HDU 1061.Rightmost Digit-规律题 or 快速幂取模

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  4. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  5. 杭电 2817 A sequence of numbers【快速幂取模】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数 ...

  6. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  7. 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) ...

  8. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  9. 九度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). ...

随机推荐

  1. xlistview的XML(头)xlistview_header

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  2. 上架app 到app store 的出现: “The IPA is invalid. It does not inlude a Payload directory.”错误处理

    今天打包上传app到app store上遇到的一个错误,在xcode6.2下提示: The IPA is  invalid. It does not inlude a Payload director ...

  3. 《day16_多线程细节_Eclipse使用》

    多线程的一些细节: 1,面试题:sleep方法和wait方法异同点是什么? 相同点:可以让线程处于冻结状态. 不同点: 1, sleep必须指定时间. wait可以指定时间,也可以不指定时间. 2, ...

  4. Unix/Linux获取进程的详细信息

    Linux的进程的信息都记录在/proc/<pid>/下面,其实常用的ps.top命令也是从这里读取信息的.常用的信息有: cmd(命令).cmdline(完整的命令行参数).envrio ...

  5. hdu 2041

    ps:这道题之前一直没思路,有大神提醒我用递推,但当时没搞清...今天做了那个小蜜蜂..才懂得用递推做这道题.. 代码: #include "stdio.h"long long d ...

  6. OpenSesame:一个能够攻击fixed-pin设备的工具

    OpenSesame是一种设备,这种设备可以通过无线技术来打开任何一个设有固定密码的车库门,我从中发现了一个攻击无线固定pin码设备的新方法. 演示视频以及详细信息: opensesame源代码:ht ...

  7. application:didFinishLaunchingWithOptions:详解

    iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...

  8. UIKit框架之UIlabel

    1.继承链:UIview:UIresponder:NSObject 2.如果你想要使UIlabel能够和用户进行互动,需要把它实例变量的属性 userInteractionEnabled改为yes 3 ...

  9. 深入分析:Android中app之间的交互(二,使用ComponentName)

    在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互. ...

  10. 封装数据库mysql, mysqli

    <?php header("content-type:text/html;charset=utf-8"); class db{    //私有的静态属性    private ...