【SSL2513】幼儿园数学题I【矩阵乘法】

在这里插入图片描述
在这里插入图片描述

分析

暴力找规律
其实题目的公式有点不完整。。。
但是看到根号5就想到斐波那契的通项公式。
百度百科
原公式如下图:
在这里插入图片描述
然后直接按斐波那契矩阵乘法搞就好了。
就像这样

上代码

/*斐波那契通项公式*/ 
#include<iostream>
#include<cstdio>
#include<algorithm> 
using namespace std;
typedef long long ll;
ll n;
const int mod=1000000007;
struct matrix
{
	ll n,m;
	ll f[20][20];
}st,A,B;

matrix operator *(matrix a,matrix b)
{
	matrix c;
	c.n=a.n;c.m=b.m;
	for(int i=1;i<=c.n;i++)
	{
		for(int j=1;j<=c.m;j++)
	    {
	    	c.f[i][j]=0;
		}
	}
	for(int k=1;k<=a.m;k++)
	{
		for(int i=1;i<=a.n;i++)
		{
			for(int j=1;j<=b.m;j++)
			{
				c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j]%mod)%mod;
			}
		}
	}
	return c;
}

void ksm(ll x)
{
	x--;
	A=st;
	while(x)
	{
		if(x&1) A=A*st;
		st=st*st;
		x>>=1;
    }
}

int main()
{
	cin>>n;
	st.n=3;st.m=3;
	st.f[1][1]=0;st.f[1][2]=1;st.f[1][3]=0;
	st.f[2][1]=1;st.f[2][2]=1;st.f[2][3]=1;
	st.f[3][1]=0;st.f[3][2]=0;st.f[3][3]=1;
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	else
	{
		B.n=1;B.m=3;
		B.f[1][1]=1;B.f[1][2]=1;B.f[1][3]=1;
		ksm(n-1);
		B=B*A;
		cout<<B.f[1][3];
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值