2020-09-29

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值