CINTA作业2

CINTA作业2

1.手动计算一下模m下a的乘法逆元

(1)m=11,a=5

0 1 5 1 0 11   → 0 1 11 1 0 5   → 2 0 10 − 2 1 1 \begin{matrix} 0 & 1 & 5\\ 1 & 0 & 11 \end{matrix} \ \rightarrow \begin{matrix} 0 & 1 & 11\\ 1 & 0 & 5 \end{matrix} \ \rightarrow \begin{matrix} 2 & 0 & 10\\ -2 & 1 & 1 \end{matrix} 0110511 0110115 2201101
5 − 1 = − 2 5^{-1}=-2 51=2

(2)m=121,a=13

1 0 13 0 1 121   → 9 0 127 0 1 121   → 0 1 121 9 − 1 − 4   → 0 1 121 270 − 30 − 120   → 270 − 30 − 120 270 − 29 1 \begin{matrix} 1 & 0 & 13\\ 0 & 1 & 121 \end{matrix} \ \rightarrow \begin{matrix} 9 & 0 & 127\\ 0 & 1 & 121 \end{matrix} \ \rightarrow \begin{matrix} 0 & 1 & 121\\ 9 & -1 & -4 \end{matrix} \ \rightarrow \begin{matrix} 0 & 1 & 121\\ 270 & -30 & -120 \end{matrix} \ \rightarrow \begin{matrix} 270 & -30 & -120\\ 270 & -29 & 1 \end{matrix} 100113121 9001127121 09111214 0270130121120 27027030291201
1 3 − 1 = 270 13^{-1}=270 131=270

(3)m=1021,a=131

1 0 131 0 1 1021   → 0 1 1021 1 − 1 − 890   → 1 − 1 − 890 1 0 131   → 1 0 131 8 − 1 27   → 8 − 1 27 − 39 5 − 4    → 273 − 35 28 8 − 1 27    → 8 − 1 27 265 − 34 1 \begin{matrix} 1 & 0 & 131\\ 0 & 1 & 1021 \end{matrix} \ \rightarrow \begin{matrix} 0 & 1 & 1021\\ 1 & -1 & -890 \end{matrix} \ \rightarrow \begin{matrix} 1 & -1 & -890\\ 1 & 0 & 131 \end{matrix} \ \rightarrow \begin{matrix} 1 & 0 & 131\\ 8 & -1 & 27 \end{matrix} \ \rightarrow \begin{matrix} 8 & -1 & 27\\ -39& 5 & -4 \end{matrix} \ \ \rightarrow \begin{matrix} 273 & -35 & 28\\ 8 & -1 & 27 \end{matrix} \ \ \rightarrow \begin{matrix} 8 & -1 & 27\\ 265 & -34 & 1 \end{matrix} 10011311021 01111021890 1110890131 180113127 83915274  27383512827  8265134271
13 1 − 1 = 265 131^{-1}=265 1311=265

2.编写程序完成模指数运算

#include <iostream>
using namespace std;
int mod_exp(int x,int y,int m)//递归 
{
	if(y==0)
	return 1;
	long long a=mod_exp(x,y/2,m);
	if((y&1)==0)
	return a*a%m;
	else return x*a*a%m;
}
int mod_exp2(int x,int y,int m)//迭代 
{
	int r=1;
	while(y>0)
	{
		if((y&1)==1)
		{
			r=(r*x)%m;
			
		}
		y=y/2;
		x=(x*x)%m;
		
	}
	return r;
}
int main()
{

	int x=0,y=0,m=0;
	cin>>x>>y>>m;
    int z1=mod_exp(x,y,m);
    int z2=mod_exp2(x,y,m);
    cout<<z1<<endl;
    cout<<z2<<endl;
}

3.快速求Fibonssi数列

#include <iostream>
using namespace std;

int Fibonassi(int n)
{
	if(n==1||n==2)
	return 1;
	if(n==0)
	return 0;
	int res[2][2]={{1,0},{0,1}};
	int temp[2][2]={{1,1},{1,0}};
	int a[2][2]={{1,1},{1,0}};
	int b[2][2]={{1,1},{1,0}};
	while(n)
	{
		if((n&1)==1)
		{
			res[0][0]=temp[0][0]*a[0][0]+temp[0][1]*a[1][0];
			res[0][1]=temp[0][0]*a[1][0]+temp[0][1]*a[1][1];
			res[1][0]=temp[1][0]*a[0][0]+temp[1][1]*a[1][0];
			res[1][1]=temp[1][0]*a[1][0]+temp[1][1]*a[1][1];
			for(int i=0;i<2;i++)//记录res矩阵 
			{
				for(int j=0;j<2;j++)
				{
					temp[i][j]=res[i][j];
				}
			}
		}
		    temp[0][0]=b[0][0]*b[0][0]+b[0][1]*b[1][0];
			temp[0][1]=b[0][0]*b[1][0]+b[0][1]*b[1][1];
			temp[1][0]=b[1][0]*b[0][0]+b[1][1]*b[1][0];
			temp[1][1]=b[1][0]*b[1][0]+b[1][1]*b[1][1];
			for(int i=0;i<2;i++)
			{
				for(int j=0;j<2;j++)
				{
					b[i][j]=temp[i][j];
				}
			}
		n>>=1;
	}
	return res[0][1];
	
}
int main()
{int n;
cin>>n;
cout<<Fibonassi(n);
//用了指针,vector想写矩阵乘法函数和矩阵n次方函数返回数组,但是都没有成功	
}

4.给定互素的正整数c和m,请证明在mod m的意义上存在唯一确定的整数值 c − 1 c^{-1} c1,它使得 c c − 1 ≡ 1 ( m o d   m ) c c^{-1}\equiv 1(mod\ m) cc11(mod m)

证明 : 先证明存在性: ∵ g c d ( c , m ) = 1 ∴ c r + m t = 1 , c r − 1 = − m t ∴ 必存在一个 c − 1 , 使得 c c − 1 ≡ 1 ( m o d   m ) 证明:\\先证明存在性:\because gcd(c,m)=1 \\ \therefore c r+m t=1,cr-1=-mt\\ \therefore 必存在一个c^{-1},使得cc^{-1} \equiv 1(mod \ m) 证明:先证明存在性:gcd(c,m)=1cr+mt=1,cr1=mt必存在一个c1,使得cc11(mod m)
证明唯一性:假设存在 a ≠ c − 1 , 使得 c a ≡ 1 ( m o d   m ) 由贝祖定理得: c a − 1 = − m t 1   c c − 1 − 1 = − m t 2 两式相减得: c ( a − c − 1 ) = − m t 1 + m t 2 c ( a − c − 1 = m ( t 2 − t 1 ) c ( a − c − 1 ) − m ( t 2 − t 1 ) = 0 可得 c ( a − c − 1 ) ≡ 0 ( m o d   m ) c a − c a ≡ 1 − 1 ( m o d   m ) a = c − 1 由上述可证 : g c d ( c , m ) = 1 , 存在唯一的 c − 1 使得 c c − 1 ≡ 1 ( m o d   m ) 证明唯一性:假设存在a \ne c^{-1},使得ca \equiv 1(mod \ m) \\ 由贝祖定理得:ca-1=-mt_{1} \ cc^{-1}-1=- mt_{2} \\ 两式相减得:c(a-c^{-1})=-mt_{1}+mt_{2} \\ c(a-c^{-1} =m(t_{2}-t_{1}) \\ c(a-c^{-1} )-m(t_{2}-t_{1})=0 \\ 可得c(a-c^{-1} ) \equiv 0(mod \ m) \\ ca-ca \equiv 1-1(mod \ m) \\ a=c^{-1} \\ 由上述可证: gcd(c,m)=1,存在唯一的c^{-1}使得c c^{-1}\equiv 1(mod\ m) 证明唯一性:假设存在a=c1,使得ca1(mod m)由贝祖定理得:ca1=mt1 cc11=mt2两式相减得:c(ac1)=mt1+mt2c(ac1=m(t2t1)c(ac1)m(t2t1)=0可得c(ac1)0(mod m)caca11(mod m)a=c1由上述可证:gcd(c,m)=1,存在唯一的c1使得cc11(mod m)

5.用Python编写求乘法逆元

def gcd(a,b):
    if a<b:
        a,b=b,a
    if b==0:
        return a
    else:
        return gcd(b, a%b)

def egcd(a,b):
    r0, r1, s0, s1 = 1, 0, 0, 1
    while (b > 0):
        q, a, b = a // b, b, a % b
        r0, r1 = r1, r0 - q * r1
        s0, s1 = s1, s0 - q * s1
    return r0, s0

def egcd2(a,b):
    t0, t1=1, 0
    while(b>0):
        q, a, b, =a // b, b, a % b
        t0, t1 =t1, t0 - q * t1
    return t0

a, b = input().split()
a, b = int(a), int(b)


flag = gcd(a,b)
if flag == 1 :
    print(egcd(a, b))
    print(egcd2(a,b))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值