快速幂矩阵连乘模板

#include <iostream>
#include <stdio.h>
#include <math.h>
#define Mod 3
using namespace std;
long long f[100];
const int MAX = 2;
typedef struct {
	long long m[MAX][MAX];
} Matrix;
Matrix P = { 0,1,
1,1 };
Matrix I = { 1,0,
0,1 };

Matrix matrixmul(Matrix a, Matrix b) //矩阵乘法
{
	int i, j, k;
	Matrix c;
	for (i = 0; i < MAX; i++)
		for (j = 0; j < MAX; j++)
		{
			c.m[i][j] = 0;
			for (k = 0; k < MAX; k++)

				c.m[i][j] += (a.m[i][k] * b.m[k][j]) /*% Mod*/;
			c.m[i][j] /*%= Mod*/;
		}
	return c;
}
Matrix quickpow(long long n)
{
	Matrix m = P, b = I;
	while (n >= 1)
	{
		if (n & 1)
			b = matrixmul(b, m);
		n = n >> 1;
		m = matrixmul(m, m);
	}
	return b;
}

posted @ 2023-07-11 17:24  Landnig_on_Mars  阅读(10)  评论(0编辑  收藏  举报  来源