快速幂相关知识点

一、普通幂

#include <iostream>

int main()
{
	int a, b;
	int result = 1;
	std::cin >> a >> b;
	for (int i = 0; i < b; i++)
	{
		result *= a;
	}
	std::cout << result << std::endl;
	return 0;
}

程序时间复杂度为O(b)

二、快速幂

#include <iostream>

int main()
{
	int a, b;
	int result = 1;
	std::cin >> a >> b;
	while (b)
	{
		if (b % 2 == 1)
		{
			result *= a;
		}
		a *= a;
		b = b >> 1;
	}
	std::cout << result << std::endl;
	return 0;
}

程序时间复杂度为O(logb)
跳跃式地进行幂的乘法,由于每次循环都将b除以2,由于整数除法会直接丢失小数部分,所以丢失的这一部分需要在if里补上去。

三、快速幂取余

#include <iostream>

int main()
{
	int a, b, c;
	int result = 1;
	std::cin >> a >> b >> c;
	b %= c;						//首先可以将b取模	
	while (b)					//每步乘法也可以取模
	{
		if (b % 2 == 1)
		{
			result = (result * a) % c;	
		}
		a = (a * a) % c;
		b = b >> 1;
	}
	std::cout << result << std::endl;
	return 0;
}

四、矩阵乘法

#include <iostream>

const int maxsize = 100;

class Matrix
{
public:
	int n;
	int mat[maxsize][maxsize];
	static void Init_matrix(Matrix* a, Matrix* b, Matrix* c);
	static void Multiply(Matrix* a, Matrix* b, Matrix* c);
};

void Matrix::Init_matrix(Matrix* a, Matrix* b, Matrix* c)
{
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{
			std::cin >> a->mat[i][j];
			std::cin >> b->mat[i][j];
			c->mat[i][j] = 0;
		}
	}
}

void Matrix::Multiply(Matrix* a, Matrix* b, Matrix* c)
{
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{
			for (int k = 0; k < a->n; k++)
			{
				c->mat[i][j] += a->mat[i][k] * b->mat[k][j];
			}
		}
	}
}

int main()
{
	Matrix* a = new Matrix;
	Matrix* b = new Matrix;
	Matrix* temp = new Matrix;
	std::cin >> a->n;
	b->n = a->n;
	Matrix::Init_matrix(a, b, temp);
	Matrix::Multiply(a, b, temp);
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{		
			std::cout << temp->mat[i][j] << " " ;
		}
		std::cout << std::endl;
	}
	delete a, b, temp;
	return 0;
}

五、矩阵快速幂(取模)
例题:
定义扩展某数列如下:
F (0) =0,F(1)=1 (n<2)
F (n) =47 * F (n-1) + 99 * F (n-2) (n>1);
设计一个函数,该函数的功能是求出F(n)%c的值,其中n < 2^62,c 是小于 500的素数。

#include <iostream>

const int maxsize = 100;

class Matrix
{
public:
	int n;
	int mat[maxsize][maxsize];
	static void Init_matrix(Matrix* a, Matrix* b);
	static void Multiply(Matrix* a, Matrix* b, Matrix* c, int d);
	static void Transfer(Matrix* a, Matrix* b);
};

void Matrix::Init_matrix(Matrix* a, Matrix* b)
{
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{
			std::cin >> a->mat[i][j];
			std::cin >> b->mat[i][j];
		}
	}
}

void Matrix::Multiply(Matrix* a, Matrix* b, Matrix* c, int d)
{
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{
			c->mat[i][j] = 0;
			for (int k = 0; k < a->n; k++)
			{
				c->mat[i][j] += (a->mat[i][k] * b->mat[k][j]) % d;
			}
			c->mat[i][j] %= d;
		}
	}
}

void Matrix::Transfer(Matrix* a, Matrix* b)
{
	for (int i = 0; i < a->n; i++)
	{
		for (int j = 0; j < a->n; j++)
		{
			a->mat[i][j] = b->mat[i][j];
		}
	}
}

int main()
{
	Matrix* a = new Matrix;
	Matrix* b = new Matrix;
	Matrix* temp1 = new Matrix;
	Matrix* temp2 = new Matrix;
	long long num, c;
	std::cin >> a->n >> num >> c;
	b->n = a->n;
	Matrix::Init_matrix(a, b);
	num--;
	while (num)
	{
		if (num % 2 == 1)
		{
			Matrix::Multiply(a, b, temp1, c);
			Matrix::Transfer(b, temp1);
		}
		Matrix::Multiply(a, a, temp2, c);
		Matrix::Transfer(a, temp2);
		num = num >> 1;
	}
	std::cout << b->mat[0][0] << std::endl;
	delete a, b, temp1, temp2;
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值