快速幂:

代码:

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. sublime text3常用快捷键

    Ctrl+L 选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行( ...

  2. [LeetCode] 4. Median of Two Sorted Arrays ☆☆☆☆☆

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. python基础---输入输出

    1.输入字符串. name=input()   or name=input('please input a string') 这样可以接收一个字符串,包括空格,都可以输入.只有回车不接受,作为结束符, ...

  4. [Luogu 2805] NOI2009 植物大战僵尸

    这题是个比较经典的最大权闭合子图,可以建图转化为最小割问题,再根据最大流最小割定理,采用任意一种最大流算法求得. 对于每个点,如果点权w为正,则从源点到这个点连一条边权为w的有向边:否则如果w为负则从 ...

  5. 【BZOJ】3895: 取石子

    [算法]博弈论+记忆化搜索 [题意]给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 [题解] 首先,若所有石子堆的石子数>1,显然总操作数为(石子 ...

  6. python进行EDA探索性数据分析

    1.查看数据的类型概况 cols = [c for c in train.columns]   #返回数据的列名到列表里 print('Number of features: {}'.format(l ...

  7. 运维开发:python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  8. Linux System.map文件【转】

    转自:http://blog.csdn.net/ysbj123/article/details/51233618 当运行GNU链接器gld(ld)时若使用了"-M"选项,或者使用n ...

  9. python基础===修改属性的值

    可以以三种不同的方式修改属性的值:直接通过实例进行修改:通过方法进行设置:通过方法进行递增(增加特定的值).下面依次介绍这些方法. class Car(): def __init__(self, ma ...

  10. rabbitmq和ons-rocketmq使用对比

    MQ,其实目的都是一样,就是应对系统的并发可以实现消峰和解耦的作用,类似于创建了一个线程异步操作,这一点可以了解一下一款优秀的并发框架(Disruptor),据说是每秒可以处理十万级数据, 目前据本人 ...