实现模块化幂算法的C ++程序
这是一个实现模块化幂运算算法的C++程序。
算法
Begin
function modular():
//参数:base,exp,mod。
//函数主体:
initialize res = 1
while (exp > 0)
if (exp mod 2 == 1)
res= (res * base) % mod
exp = exp left shift 1
base = (base * base) % mod
return res.
End示例
#include <iostream>
using namespace std;
long long modular(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res= (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
int main() {
long long b, e;
int mod;
cout<<"Enter Base : ";
cin>>b;
cout<<"Enter Exponent: ";
cin>>e;
cout<<"Enter Modular Value: ";
cin>>mod;
cout<<modular(b, e , mod);
return 0;
}输出结果
Enter Base : 7 Enter Exponent: 6 Enter Modular Value: 26 25
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短