矩阵乘法+快速幂+序列递推公式

1250: Gobonacci

时间限制: 1 Sec  内存限制: 128 MB
提交: 75  解决: 11
[ 提交][ 状态][ 讨论版]

题目描述

      Fibonacci sequence is well-known to us ,f(0)=1,f(1)=1, f(n) = f(n-1)+f(n-2), how nice the recursion! In addition, the is exponential growth in series. Obviously, Goagain was not content with the fact that the series didn’t increased as rapidly as he had expected, therefore, he designed such a function

g(0) = a   g(1)= b    g(2)=c

g(n)=3*g(n-1)+2*g(n-2)+g(n-3)      (n>=2)

     Besides, the function was named after Goagain series. What made him distracted was that he could hardly calculate the series.

     By the way , goagain without any festival fuck named it Gobonacci sequence

输入

Multiple sets of input data, each set including four positive integers a, b, c ,n  ( a, b, c <= 100 , 0<=  n <2^60)

输出

Please output the value of g(n), considering the value of g(n) is far too large, output g(n) %100000007  is okay.

样例输入

1 1 1 33 2 1 3

样例输出

610
 
 
 
 
 
 
#include<iostream>
using namespace std;
struct m{long long  int a[3][3]; };
long long int big = 100000007;
m aa = { 0, 1, 0, 0, 0, 1, 1, 2, 3 };
m mul(m a, m b){
	int i, j, k;
	m c;
	for (i = 0; i < 3;i++)
	for (j = 0; j < 3; j++)
	{
		c.a[i][j] = 0;
		for (k = 0; k < 3; k++)
			c.a[i][j] +=( (a.a[i][k]%big) * (b.a[k][j]%big))%big;
		c.a[i][j] %= big;
	}
	return c;
}
m power(m a, long long int n){
	if (n == 1)return a;
	m b = power(a, n / 2);
	m c = mul(b, b);
	if (n % 2 == 1)c = mul(a, c);
	return c;
}
int main(){
	freopen("in.txt", "r", stdin);
	long long int a, b, c, n;
	while (cin >> a >> b >> c >> n){
		if (n == 0){ cout << a << endl; continue; }
		if (n == 1){ cout << b << endl; continue; }
		if (n == 2){ cout << c << endl; continue; }
		m ans = power(aa, n-2);
		cout << (ans.a[2][0] * a + ans.a[2][1] * b + ans.a[2][2] * c) % big << endl;
	}
}


转载于:https://www.cnblogs.com/weiyinfu/p/5013903.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值