#include <iostream>
using namespace std;
#define MAXLEN 1000
void Dec_To_Bin(int Dec,int *Bin,int *len)
{
int i=0;
while(Dec)
{
Bin[i]=Dec%2;
Dec/=2;
i++;
}
*len=i;
}
int ModRepeatSquare(int b,int n,int m)
{
int a=1,t=b;
int len,Bin[MAXLEN];
Dec_To_Bin(n,Bin,&len);
for(int i=0;i<len;i++)
{
if(!Bin[i])
{
cout<<"n"<<i<<"="<<Bin[i]<<"\t";
a=a;
b=b*b%m;
cout<<"a"<<i<<"="<<a<<" \t"<<"b"<<i<<"="<<b<<endl;
}
else
{
cout<<"n"<<i<<"="<<Bin[i]<<"\t";
a=a*b%m;
b=b*b%m;
cout<<"a"<<i<<"="<<a<<" \t"<<"b"<<i<<"="<<b<<endl;
}
}
return a;
}
int main()
{
int b,n,m;
cout<<"Please input b,n,m:"<<endl;
cin>>b>>n>>m;
cout<<b<<"^"<<n<<"="<<ModRepeatSquare(b,n,m)<<"(mod "<<m<<")";
return 0;
}
模重复平方计算
最新推荐文章于 2020-10-30 22:06:58 发布