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. Hibernate中的继承映射

    1.继承映射 继承映射分为两种情况:简单继承映射和继承映射. 在简单继承映射中,每个子类都要写一个映射文件. 在继承映射中,只使用一个映射文件.继承映射分为三种情况: 所有子类映射到一张表 需要使用鉴 ...

  2. java基础之 反射

    首先,我们在开始前提出一个问题: 1.在运行时,对于一个java类,能否知道属性和方法:能否去调用它的任意方法? 答案是肯定的. 本节所有目录如下: 什么是JAVA的反射机制 JDK中提供的Refle ...

  3. 修改Azure Website 移动服务 默认时区

    Azure Website 默认时区为国际标准时间,对中国用户来说不太方便友好,如何设置成北京时间呢? 打开Azure Website的“配置”页,找到“应用设置”节点. 在应用设置中添加设置项,密钥 ...

  4. Julia中文教程资源.txt

    Julia中文教程资源.txt 2016年3月28日 05:18:32 codegay 本文更新在这里: https://github.com/FGFW/julia-science-and-techn ...

  5. RTL2832U+R820电视棒跟踪飞机轨迹教程(ADS-B)

    Ubuntu 14.04.3 amd64 apt-get install git apt--dev 安装rtl-sdr git clone git://git.osmocom.org/rtl-sdr. ...

  6. php大力力 [036节] 后台系统的登录页面界面做完啦

    php大力力 [036节] 后台系统的登录页面界面做完啦 我认为做的不错,我就先不上截图啦 要你的祝福 分布注册 Twitter Login Or Signup Form 藤藤每日一练——172个Ic ...

  7. android的 makefile里 的常用 宏定义

    在Android编译框架中,把许多固定的.反复用到的目录路径定义为 宏变量,常用 宏 如下: out/target/product/xxx的宏即为:PRODUCT_OUT out/target/pro ...

  8. HDOJ-三部曲一(搜索、数学)-1005-Dungeon Master

    Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  9. 【Zhejiang University PATest】02-1. Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  10. IOS 作业项目(4)步步完成 画图 程序(中续)

    一,程序布局整理 前言://1,程序启动//2,程序流程框架//3,程序界面一致//4,程序界面功能, //这里只做页面的固定功能, //在首次创建界面时,我们会指定好固定事件触发前的固定方法 //至 ...