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 →2−201101
5
−
1
=
−
2
5^{-1}=-2
5−1=−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 →091−1121−4 →02701−30121−120 →270270−30−29−1201
1
3
−
1
=
270
13^{-1}=270
13−1=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 →011−11021−890 →11−10−890131 →180−113127 →8−39−1527−4 →2738−35−12827 →8265−1−34271
13
1
−
1
=
265
131^{-1}=265
131−1=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} c−1,它使得 c c − 1 ≡ 1 ( m o d m ) c c^{-1}\equiv 1(mod\ m) cc−1≡1(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)=1∴cr+mt=1,cr−1=−mt∴必存在一个c−1,使得cc−1≡1(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=c−1,使得ca≡1(mod m)由贝祖定理得:ca−1=−mt1 cc−1−1=−mt2两式相减得:c(a−c−1)=−mt1+mt2c(a−c−1=m(t2−t1)c(a−c−1)−m(t2−t1)=0可得c(a−c−1)≡0(mod m)ca−ca≡1−1(mod m)a=c−1由上述可证:gcd(c,m)=1,存在唯一的c−1使得cc−1≡1(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))