K - 233 Matrix HDU - 5015

本文介绍了一种名为233矩阵的问题,通过矩阵快速幂的方法解决给定矩阵中特定位置的元素。博主分享了如何利用递归和矩阵乘法优化计算过程,并给出了一个C++代码示例。关键在于避免重复计算,最终目标是求解a1,0到an,0位置的最小值对1e7+7取余数。
摘要由CSDN通过智能技术生成

K - 233 Matrix HDU - 5015
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 … in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333… (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333…) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,…,an,0, could you tell me an,m in the 233 matrix?
InputThere are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,…,an,0(0 ≤ ai,0 < 231).OutputFor each case, output an,m mod 10000007.Sample Input

1 1
1
2 2
0 0
3 7
23 47 16
Sample Output

234
2799
72937

题解

矩阵快速幂,主要是找出重复计算的矩阵

参考大佬写法

#include<bits\stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e7+7;
ll n, m, ant[15];
typedef struct node {
	ll e[15][15];
	void Init(){
		memset(e, 0, sizeof(e));
		for(int i = 0; i < n+2; i++) e[i][i] = 1; 
	}
}Mar;

Mar ans, mp;

Mar Mut(Mar a, Mar b) {
	Mar c;
	memset(c.e, 0, sizeof(c.e));
	for(int i = 0; i < n+2; i++) {
		for(int j = 0; j < n+2; j++) {
			for(int k = 0; k < n+2; k++) {
				c.e[i][j] += (a.e[i][k] * b.e[k][j])%mod;
			}
		c.e[i][j] %= mod; // 一定要再mod一次 
		}
	}
	return c;
}

Mar quick_mod(Mar a, int power) {
	Mar re;
	re.Init();
	while(power) {
		if(power&1) re = Mut(re, a);
		a = Mut(a, a);
		power >>= 1;
	}
	return re;
}

int main() {
	freopen("test.in", "r", stdin);
	while(cin >> n >> m) {
		ll wsy = 0;
		ant[0] = 23;

		for(int i = 1; i < n+1; i++) cin >> ant[i];
		ant[n+1] = 3;
		cout << "*" << endl;
		memset(mp.e, 0, sizeof(mp.e));
		for(int i = 0; i < n+1; i++) mp.e[i][0] = 10;
		for(int i = 0; i < n+2; i++) mp.e[i][n+1] = 1;
		for(int i = 0; i < n+1; i++)
			for(int j = i; j >= 1; j--)
				mp.e[i][j] = 1;				
		ans = quick_mod(mp, m);
		
		for(int i = 0; i < n+2; i++)
			wsy += (ans.e[n][i] * ant[i])%mod;
		wsy %= mod;
		cout << wsy << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值