CINTA作业二

CINTA作业二

题目一:

手动计算一下模m下a的乘法逆元。(a)m=11,a=5;(b)m=121,a=13;©m=1021,a=131

在这里插入图片描述

题目二:

编写C语言程序完成模指数运算,既给定整数x,y和m为输入,计算并返回值xy mod m

#include <iostream>
using namespace std;

int power(int x, int y, int m) {
	int temp = 1;
	while (y != 1) {
		if (y % 2)
			temp = (x * temp) % m;
		y /= 2;
		x = (x * x) % m;
	}
	return (x * temp) % m;
}

int main() {
	int x, y, m;
	cin >> x >> y >> m;
	cout << power(x, y, m);
	return 0;
}

题目三:

定义 Fibonacci 数列如下: F ( 0 ) = 0 , F ( 1 ) = 1 F(0)=0,F(1)=1 F(0)=0,F(1)=1,且对于 n ⩽ 2 , F ( n ) = F ( n − 1 ) + F ( n − 2 ) n\leqslant 2,F(n)=F(n-1)+F(n-2) n2,F(n)=F(n1)+F(n2)。所以,该数列是: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , . . 0,1,1,2,3,5,8,13,21,.. 0,1,1,2,3,5,8,13,21,..。如何能快速地求出 F ( n ) F(n) F(n) 呢?很幸运,我们有以下等式:

[ 1 1 1 0 ] n = [ F ( n + 1 ) F ( n ) F ( n ) F ( n − 1 ) ] \begin{equation} \left[ \begin{matrix} 1&1\\ 1&0 \end{matrix} \right]^n= \left[ \begin{matrix} F(n+1)&F(n)\\ F(n)&F(n-1) \end{matrix} \right] \end{equation} [1110]n=[F(n+1)F(n)F(n)F(n1)]

虽然,看上去该算法需要一次矩阵的指数运算,但是借助快速指数运算的方法,这里可以产生一个快速求解 F ( n ) F(n) F(n) 的算法。请给出算法,并编程实现,C 语言或者 Python 都可以。

#include <iostream>
using namespace std;

int *mat22_mul(int *a, int *b) {
	int *c = new int[4];
	for (int i = 0; i <= 3; i++)
		c[i] = 0;
	for (int i = 0; i <= 3; i++) {
		for (int j = 0; j <= 1; j++) {
			//cout << i << ' ' << a[i / 2 * 2 + j] * b[j * 2 + i % 2] << endl;
			c[i] += a[i / 2 * 2 + j] * b[j * 2 + i % 2];
		}
	}
	return c;
}

int *mat22_power(int *a, int n) {
	int *temp = new int[4] {1, 0, 0, 1};
	while (n != 1) {
		if (n % 2) {
			int *temp_t = mat22_mul(temp, a);
			free(temp);
			temp = temp_t;
		}
		n /= 2;
//		cout << n << endl;
//		for (int i = 0; i <= 3; i++)
//			cout << a[i] << ' ';
//		cout << endl;
		int *a_t = mat22_mul(a, a);
		free(a);
		a = a_t;
	}
	int *a_t = mat22_mul(temp, a);
	free(a);
	a = a_t;
//	cout << n << endl;
//	for (int i = 0; i <= 3; i++)
//		cout << a[i] << ' ';
//	cout << endl;
	return a;
}

int main() {
	int *a = new int[4] {1, 1, 1, 0}, n;
	cin >> n;
	if (n == 1) {
		cout << 0;
		return 0;
	}
	n--;
	int *a_t = mat22_power(a, n);
	free(a);
	a = a_t;
	cout << a[1];
	return 0;
}

题目四:

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

证明:

由于c,m互质,所以 gcd(c,m)=1 由扩展欧几里得原理得一定存在cr+sm=1,所以 c c c$ c^{-1}$ $\equiv$1 (mod m)一定存在

由于乘法逆元要在[0,m)中,r的可替代值是r+k(ck=nm),所以n!=0,所以k大于等于m,所以只有唯一的r ∈ \in [0,m)

题目五:

编写一个 Python 程序计算乘法逆元,即输入互素的正整数 c 和 m,返回 $ c^{-1}$,使得 c c c$ c^{-1}$ ≡ (mod m)。要求:

def Egcd(c,m):
    x1,x2,x3,y1,y2,y3=1,0,c,0,1,m
    if(c<m):
        x1,x2,x3,y1,y2,y3=y1,y2,y3,x1,x2,x3
    while(y3!=1):
        t=x3//y3
        x1,x2,x3,y1,y2,y3=y1,y2,y3,x1-t*y1,x2-t*y2,x3-t*y3
    return y1
print("请输入一个整数c")
c = int(input())
print("请输入模m")
m = int(input())
print(Egcd(c,m))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值