快速幂取模
来源:互联网 发布:手机淘宝7.1.1版本 编辑:程序博客网 时间:2024/04/28 14:25
快速幂取模问题也是经常遇到的问题,其实它不过是用到了二分的思想,逐渐除2的过程。这也算是一个基础的工具,聪明的你一定会非常容易的理解它,对不对?而且我相信你也一定会勤劳的将他作为模板记下的!
给出三个数啊,a,b,c,让你求出a^b%c;的值。
代码:
#include <iostream>
using namespace std;
long long quickpow(long long x,long long n,long long z)
{
long long res = 1;
while(n>0)
{
if(n & 1) res = ((res%z)*(x%z))%z ;
x = ((x%z)*(x%z))%z ;
n=n >>1;
}
return res;
}
int main()
{
long x,y,k,z,n;
while(cin>>x>>y>>z)
{
k=quickpow(x,y,z);
cout<<k<<endl;
}
}
其中&与>>是位移运算符(相关知识就不多介绍了了,详情请看)
1 0
- 快速幂,快速幂取模
- 快速幂 + 快速幂取模
- 快速幂 快速幂取模
- 快速幂 快速幂取模
- 【快速幂】【快速幂取模】
- 快速幂,快速幂取模,矩阵快速幂
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- 快速幂取模
- php基础-相对路径与绝对路径
- JAVA exception异常处理+I/O操作读写文件 笔记
- 用STL进行字符串的分割
- window ant安装与配置
- Leetcode-Power of 2,3,4
- 快速幂取模
- 10个关于JavaScript小技巧
- 1003 我要通过!(20)
- 服务器U盘安装安装centos
- 获取联系人信息
- 素数的筛法,n*long(n)和O(n)的写法
- <LeetCode OJ> 105. Construct Binary Tree from Preorder and Inorder Traversal
- 汇编学习(十三)8086汇编拾遗 (12)
- c++上机实验7项目1