P962 矩阵快速幂初级应用:斐波那契数列

P962 矩阵快速幂初级应用:斐波那契数列,第1张

原题链接:

洛谷 P1962https://www.luogu.com.cn/problem/P1962

解题思路:用到了矩阵快速幂的想法,首先就是推导出 矩阵递推式。


 

 先来几个模板: 矩阵乘法模板:

缺点:运算了m*n*s次

#include 
using namespace std;
const int mod = 1e7 + 10, N = 100;
int ans[N][N], a[N][N], b[N][N];
int m, n, s;
void mul(int a[][N], int b[][N])
{
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= s; j++)
			for (int k = 1; k <= n; k++)
				ans[i][j] += a[i][k] * b[k][j];
}
int main() {
	cin >> m >> n;
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];
	cin >> n >> s;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= s; j++)
			cin >> b[i][j];
	mul(a, b);
	cout << endl;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= s; j++)
			cout << ans[i][j] << " ";
		cout << endl;
	}
	return 0;
}
轻微优化速度:
#include 
using namespace std;
const int N = 110;
int ans[N][N], a[N][N], b[N][N], tmp;
int n, m, s;
void mul(int a[][N], int b[][N]) {
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= m; i++) {
			tmp = a[i][k];
			for (int j = 1; j <= s; j++)
				ans[i][j] += tmp * b[k][j];
		}
	}
}
int main() {
	cin >> m >> n;
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];
	cin >> n >> s;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= s; j++)
			cin >> b[i][j];
	mul(a, b);
	cout << endl;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= s; j++)
			cout << ans[i][j] << " ";
		cout << endl;
	}
	return 0;
}
矩阵快速幂模板
#include 
#include 
#include 
using namespace std;
const int N = 110;
typedef long long LL;
LL a, b;
struct Mac {
	LL mac[N][N];
}ans, res,start;
Mac mul(Mac x, Mac y) {
	Mac z;
	memset(z.mac, 0, sizeof z.mac);
	for (int k = 1; k <= a; k++)
		for (int i = 1; i <= a; i++)
			for (int j = 1; j <= a; j++)
				z.mac[i][j] += x.mac[i][k] * y.mac[k][j];
	return z;
}
Mac qmi(Mac x, LL y) {
	for (int i = 1; i <= a; i++)res.mac[i][i] = 1;
	while (y) {
		if (y & 1)res = mul(res, x);
		y >>= 1;
		x = mul(x, x);
	}
	return res;
}
int main() {
	cin >> a >> b;
	for (int i = 1; i <= a; i++)
		for (int j = 1; j <= a; j++)
			cin >> start.mac[i][j];
	ans = qmi(start, b);
	for (int i = 1; i <= a; i++) {
		for (int j = 1; j <= a; j++)
			cout << ans.mac[i][j] << " ";
		cout << endl;
	}
}
本题题解:(最后输出一定一定要%mod!)
#include 
#include 
using namespace std;
const int N = 10, mod = 1e9 + 7;
typedef long long LL;
LL n, m[N][N], fn[N][N];
struct Mac {
	LL mac[N][N];
}ans, key, res;
Mac mul(Mac x, Mac y) {
	Mac z;
	memset(z.mac, 0, sizeof z.mac);
	for (int k = 1; k <= 2; k++)
		for (int i = 1; i <= 2; i++)
			for (int j = 1; j <= 2; j++)
				z.mac[i][j] = (z.mac[i][j]+x.mac[i][k] * y.mac[k][j])%mod;
	return z;
}
void qmi() {
	for (int i = 1; i <= 2; i++)ans.mac[i][i] = 1;
	while (n)
	{
		if (n & 1)ans = mul(ans, key);
		n >>= 1;
		key = mul(key, key);
	}
}
int main() {
	cin >> n;
	if (n <= 2)cout << "1" << endl;
	else {
		fn[1][1] = 1, fn[1][2] = 1;
		for (int i = 1; i <= 2; i++)
			for (int j = 1; j <= 2; j++)
				key.mac[i][j] = 1;
		key.mac[2][2] = 0;
		n -= 2;
		qmi();
		for (int k = 1; k <= 2; k++)
			for (int i = 1; i <= 2; i++)
				for (int j = 1; j <= 2; j++)
					m[i][j] += (fn[i][k] % mod*ans.mac[k][j]%mod )%mod;
		cout << m[1][1]%mod << endl;
	}
	return 0;
}
瞻仰一下dalao的代码码风:
#include 
#include 
using namespace std;
const int mod = 1e9+7;
typedef long long LL;
struct Matrix {
    int a[3][3];
    Matrix() { memset(a, 0, sizeof a); } // 构造函数,矩阵初始化全零
    Matrix operator*(const Matrix& b) const {
        Matrix res;
        for (int i = 1; i <= 2; i++)
            for (int j = 1; j <= 2; j++)
                for (int k = 1; k <= 2; k++)
                    res.a[i][j] = (res.a[i][j] + a[i][k] * b.a[k][j]) % mod;
        return res;
    }
} ans, base;

void init() { // 初始化 ans、base 矩阵
    base.a[1][1] = base.a[1][2] = base.a[2][1] = 1;
    ans.a[1][1] = ans.a[1][2] = 1;
}

void qpow(int b) { // 求
    while (b) {
        if (b & 1) ans = ans * base;
        base = base * base;
        b >>= 1;
    }
}

int main() {
    LL n;
    cin >> n;
    if (n <= 2) cout << "1" << endl;
    else {
        init();
        qpow(n - 2);
        cout << ans.a[1][1] % mod;
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/565127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)

保存