快速幂:

代码:

ll pow_mod(ll a,ll b){
     ll ans=;
     while(b){
         if(b%==){
             ans=ans*a%mod;
         }
         a=a*a%mod;
         b=b/;                              //这里是转化为二进制之后的进位---左进位
     }
     return ans;
 }

例子:

2^10       1 0 1 0 a=2,b=10   0-->a=a*a;a=4 进位为1-->ans=4;a=16;

进位为0-->a=256;

进位为1-->ans=4*256=1024;
  2^8         1 0 0 0 a=2,b=8    a=a*a  a=4 a=16  a=256 ans=ans*a;
  2^11       1 0 1 1 a=2,b=11   ans=2;a=4;ans=8;a=16;a=256;ans=8*256;

写了一道题:

这道题要在快速幂中取模,利用公式a*b%c=((a%c)*b)%c,这样每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化。

HDU1097A hard puzzle

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
 
Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
 
Output
For each test case, you should output the a^b's last digit number.
 
Sample Input
7 66
8 800
 
Sample Output
9
6
 
 
 
 
代码;
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull mod=1e9;
ull pow(ull a,ull b){
ull ans=;
   while(b!=){
if(b%==)
ans=ans*a%mod;
a=a*a%mod;
b=b/;
}
return ans;
}
int main(){
ull a,b;
while(~scanf("%llu%llu",&a,&b)){
ull cnt=pow(a,b);
ull ans=cnt%;
printf("%llu\n",ans);
}
return ;
}
 

HDU 1097.A hard puzzle-快速幂/取模的更多相关文章

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

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

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

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

  3. HDU 1061 Rightmost Digit (快速幂取模)

    题意:给定一个数,求n^n的个位数. 析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的, 找一下就OK了. 代码如下: #include <iostrea ...

  4. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. 实体框架(Entity Framework)快速入门

    实体 框架 (Entity Framework )简介 实体框架Entity Framework 是 ADO .NET 中的一组支持 开发 面向数据的软件应用程序的技术.是微软的一个ORM框架. OR ...

  2. HDU5875 Function

    题意:给定序列,有m个区间的询问,求每个询问a[l]%a[l+1]...%a[r]后的值.(N<=10^5) 思路:这题如果使用线段树,可能会由于姿势和卡常数原因TLE,由于数据好像比较奇怪(? ...

  3. LightOJ 1218 概率水题(几何分布)

    题意:给你一个n面骰子,问你投出所有面需要的次数的期望值是多少. 题解:放在过去估计秒解,结果现在自己想好久,还查了下,有人用极限证明...实际上仔细想想这种情况投出与前面不一样的概率p的倒数就是次数 ...

  4. 2015/8/18 Python基本使用(2)

    关于判断和循环语句 Python的判断和循环语句非常直观,读起来很接近自然语言. 判断语句if标准的if语句是如下结构: if expression: if_suite 如果expression的表达 ...

  5. linux命令查看服务器的型号、序列号、内存插槽数(转)

    1,查看服务器型号.序列号: dmidecode|grep "System Information" -A9|egrep  "Manufacturer|Product|S ...

  6. ionic@2.0 beta版本安装指南

    由于访问npm官方源下载ionic速度缓慢,淘宝提供了npm源,方便国内人士访问. 1.通过config命令 npm config set registry https://registry.npm. ...

  7. 彻底找到 Tomcat 启动速度慢的元凶 /dev/random

    参考  http://blog.csdn.net/u013939884/article/details/72860358

  8. NGINX: 返回大 JSON 数据不完整的问题

    说明: 内容全部来自 [ CSDN 金玮良 ] nginx 返回数据不完整的问题 当nginx 遇到大数据流时,会把数据先放在自己的缓冲区,然后一并发给客户端. 那如果这个结论成立, 那一次请求的数据 ...

  9. Spring boot集成RabbitMQ(山东数漫江湖)

    RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...

  10. bzoj 1503 平衡树

    我们可以用一颗平衡树维护每个人的工资,因为工资的变化会影响到后面所有的人,所以我们打一个标签,向平衡树里插入的时候减去这个标签的值,这样代表改变了之后的零点,,这样维护这个标签就好了,输出的时候要加上 ...