哈理工邀请赛 F.Fibonacci Again (矩阵快速幂)
2016-12-04 17:35
337 查看
题目地址:点击打开链接
比赛的时候打表找到了规律,6个一循环,可以前没做过类似的周期递推的求矩阵快速幂..没构造出来。
以下参考博客:点击打开链接
解题思路:解决本题的一个关键就是发现其表达式和原来的斐波那契数列的不同,通过打表分析观察,可以发现一个以6为周期的循环。
通过这些表达式就可以将这道问题转化成常见的矩阵快速幂问题了,因为后面存在常数项,所以需要构造一个7*7的矩阵。
(这图最右那矩阵下面少了个1)
由此就可以套用矩阵快速幂的思路解决此题了~
代码:
Description
There are another kind of Fibonacci numbers: F(0) = 7,
F(1) = 11, F(n) = (F(n-1) + F(n-2)) xor (n&1) (n>=2).
Input
Input consists of a sequence of lines, each containing
an integer n. (0<=n < 1,000,000,000).
Output
Output F(n) mod 1000000007.
Sample Input
2 3 4 5
- 10 -
Sample Output
18
28
46
75
比赛的时候打表找到了规律,6个一循环,可以前没做过类似的周期递推的求矩阵快速幂..没构造出来。
以下参考博客:点击打开链接
解题思路:解决本题的一个关键就是发现其表达式和原来的斐波那契数列的不同,通过打表分析观察,可以发现一个以6为周期的循环。
通过这些表达式就可以将这道问题转化成常见的矩阵快速幂问题了,因为后面存在常数项,所以需要构造一个7*7的矩阵。
(这图最右那矩阵下面少了个1)
由此就可以套用矩阵快速幂的思路解决此题了~
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int mod = 1e9+7; int f[7] = {7, 11, 18, 28, 46, 75, 1}; struct node { ll s[7][7]; void init() { memset(s, 0, sizeof(s)); } }; node mul(node a, node b) { node t; t.init(); for(int i = 0; i < 7; i++) for(int j = 0; j < 7; j++) for(int k = 0; k < 7; k++) t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j])%mod; return t; } node mt_pow(node p, int n) { node q; q.init(); for(int i = 0; i < 7; i++) q.s[i][i] = 1; while(n) { if(n&1) q = mul(p, q); p = mul(p, p); n /= 2; } return q; } void show(node a) { for(int i = 0; i < 7; i++) for(int j = 0; j < 7; j++) printf("%d%c", a.s[i][j], j==6 ? '\n' : ' '); } int main(void) { int n; while(cin >> n) { node base; base.init(); base.s[0][4] = 1, base.s[0][5] = 1, base.s[0][6] = 0; base.s[1][4] = 1, base.s[1][5] = 2, base.s[1][6] = 1; base.s[2][4] = 2, base.s[2][5] = 3, base.s[2][6] = 1; base.s[3][4] = 3, base.s[3][5] = 5, base.s[3][6] = 1; base.s[4][4] = 5, base.s[4][5] = 8, base.s[4][6] = 2; base.s[5][4] = 8, base.s[5][5] = 13, base.s[5][6] = 4; base.s[6][4] = 0, base.s[6][5] = 0, base.s[6][6] = 1; node temp = mt_pow(base, n/6); // show(temp); ll ans[7] = {0}; for(int i = 0; i < 7; i++) for(int j = 0; j < 7; j++) ans[i] = (ans[i]+temp.s[i][j]*f[j])%mod; printf("%lld\n", ans[n%6]); } return 0; }
Description
There are another kind of Fibonacci numbers: F(0) = 7,
F(1) = 11, F(n) = (F(n-1) + F(n-2)) xor (n&1) (n>=2).
Input
Input consists of a sequence of lines, each containing
an integer n. (0<=n < 1,000,000,000).
Output
Output F(n) mod 1000000007.
Sample Input
2 3 4 5
- 10 -
Sample Output
18
28
46
75
相关文章推荐
- 哈尔滨理工大学软件学院ACM程序设计全国邀请赛(网络同步赛)F Fibonacci Again 矩阵快速幂
- HIT OJ 2060 Fibonacci Problem Again(矩阵快速幂)
- hrbust/哈理工oj 1787 New Fibonacci Number【欧拉降幂+矩阵快速幂】
- poj 3070 Fibonacci(矩阵快速幂)
- Another kind of Fibonacci(hdu3306)矩阵快速幂
- POJ 3070 Fibonacci 矩阵快速幂
- POJ 3070 Fibonacci 矩阵快速幂
- Fibonacci - POJ 3070 矩阵乘法快速幂
- 从sicily Fibonacci 问题出发解决矩阵快速幂求解斐波那契问题
- POJ 3070 Fibonacci (初学矩阵快速幂)
- 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)
- Fibonacci--poj3070(矩阵快速幂)
- WOJ1540 Fibonacci(数学推导,矩阵快速幂)
- poj 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)
- poj 3070 Fibonacci(矩阵快速幂模板,斐波那契)
- POJ3070 - Fibonacci (矩阵快速幂)
- hdu Covering 2017广西邀请赛 矩阵快速幂
- 【POJ 3070】Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)