博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
矩阵乘法+快速幂+序列递推公式
阅读量:6473 次
发布时间:2019-06-23

本文共 1723 字,大约阅读时间需要 5 分钟。

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
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; }}

转载地址:http://xopko.baihongyu.com/

你可能感兴趣的文章
AJAX POST&跨域 解决方案 - CORS
查看>>
开篇,博客的申请理由
查看>>
Servlet 技术全总结 (已完成,不定期增加内容)
查看>>
[JSOI2008]星球大战starwar BZOJ1015
查看>>
centos 7 部署LDAP服务
查看>>
iOS项目分层
查看>>
IntelliJ IDEA 注册码
查看>>
String字符串的截取
查看>>
DynamoDB Local for Desktop Development
查看>>
Shell编程-环境变量配置文件
查看>>
[Unity3d]DrawCall优化手记
查看>>
Struts2和Spring MVC的区别
查看>>
理解Javascript参数中的arguments对象
查看>>
p2:千行代码入门python
查看>>
bzoj1106[POI2007]立方体大作战tet*
查看>>
spring boot configuration annotation processor not found in classpath问题解决
查看>>
由中序遍历和后序遍历求前序遍历
查看>>
我学习参考的网址
查看>>
[Processing]点到线段的最小距离
查看>>
GitHub使用教程、注册与安装
查看>>