快速幂:

代码:

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. android Handler post sendMessage

    Handler 为Android操作系统中的线程通信工具,包为android.os.Handler. 与Handler绑定的有两个队列,一个为消息队列,另一个为线程队列.Handler可以通过这两个队 ...

  2. 实现拷贝函数(strcpy)

    #include <stdio.h> #include <stdlib.h> // 函数声明 char *mystrcpy(char *object, char *source ...

  3. 21、python操作redis的模块?

    什么是redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  4. 如何彻底关闭退出vmware虚拟机

    如何彻底关闭退出vmware虚拟机 每次使用虚拟机之后退出时,它都会在系统托盘区留下一个虚拟机图标,该如何彻底关闭退出vmware虚拟机呢? 首先我们需要运行一下虚拟机程序 1:我们如果要对虚拟机进行 ...

  5. webpack自动化构建你的项目

    1.读万卷书,行万里路. 2.书山有路勤为径,学海无涯苦作舟. 技术段: 相信很多刚接触前端的小伙伴,对一些自动化工具会感觉无可下手.现在前端的发展的势头,势必和后台形成一个对立面,独挡一面. 这篇文 ...

  6. Caffe学习笔记4图像特征进行可视化

    Caffe学习笔记4图像特征进行可视化 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit201 ...

  7. Linux-进程间通信(五): 网络套接字

    不想说话,坑太深:持续学习网络编程中...

  8. python基础===成员访问__len__()和__getitem__()

    class A: def __init__(self,*args): self.name = arg pass def __len__(self): return len(self.name) a = ...

  9. xtrabackup 安装、备份和恢复

    xtrabackup 版本对应: 2.4 专针对 5.7 开发的,兼容 5.6, 5.5 2.3 针对 5.6 开发的,兼容5.5 2.2 针对5.5 开发的 安装包下载: wget https:// ...

  10. sicily 1001. Fibonacci 2

    1001. Fibonacci 2   Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + F ...