#include<iostream>
using namespace std;
int egcd(int x,int y)
{int r0=1,s1=1,r1=0,s0=0,q,tem;
while(y)
{
q=x/y;tem=x;x=y;
y=tem%y;tem=r0;
r0=r1;r1=tem-q*r1;
tem=s0;s0=s1;
s1=tem-q*s1;
}
return r0;
}
int gcd(int x,int y)
{int tem;
while(y!=0)
{
tem=y;
y=x%y;
x=tem;
}
return tem ;
}
int main()
{
int a,m,r;
cin>>a>>m;
if(gcd(a,m)!=1)
{
cout<<"该式无乘法逆元,请重新输入";
return 1;
}
r= egcd(a,m);
cout<<r;
return 0;
}
作业2:
#include<iostream>
#include<cmath>
using namespace std;
unsigned int mod_exp(int x,int y,int m)
{int res=1;
while(y>0)
{if((y&1)==1)
{res=(res*x)%m;
}
y=y/2;
x=(x*x)%m;
}
return res;
}
int main()
{int x,y,m,s;
cin>>x,y,m;
s=mod_exp(x,y,m) ;
cout<<s;
return 0;
}