[HDU2855]Fibonacci Check-up
题目:Fibonacci Check-up
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855
分析:
1)二项式展开:$(x+1)^n = \sum^n_{k=0}{C^k_n * x^k}$
2)Fibonacci数列可以写为:$ \left[ \begin{array}{cc} 0 & 1 \\ 1 & 1 \end{array} \right]^n$的左下角项。
3)构造矩阵$ T = Fib+E = \left[ \begin{array}{cc} 0 & 1 \\ 1 & 1 \end{array} \right] + \left[ \begin{array}{cc} 1 & 0 \\ 0 & 1 \end{array} \right] = \left[ \begin{array}{cc} 1 & 1 \\ 1 & 2 \end{array} \right]$。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
int MOD;
struct Matrix{
LL a[][];
void init(int f){
memset(a,,sizeof a);
if(f==-)return;
for(int i=;i<;++i)a[i][i]=;
}
};
Matrix operator*(Matrix& A,Matrix& B){
Matrix C;C.init(-);
for(int i=;i<;++i)
for(int j=;j<;++j)
for(int k=;k<;++k){
C.a[i][j]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=MOD;
}
return C;
}
Matrix operator^(Matrix A,int n){
Matrix Rt;Rt.init();
for(;n;n>>=){
if(n&)Rt=Rt*A;
A=A*A;
}
return Rt;
}
int main(){
int n,Case;scanf("%d",&Case);
Matrix A,T;
T.a[][]=;T.a[][]=;
T.a[][]=;T.a[][]=; for(;Case--;){
scanf("%d%d",&n,&MOD);
A=T^n;
LL ans=A.a[][];
printf("%lld\n",ans%MOD);
} return ;
}
4)$\sum^n_{k=0}{C^k_n * f(k)} = f(2*n) $
5)证明:$ \sum^n_{k=0}{C^k_n * f(k)} $
= $ \sum^n_{k=0}{ C^k_n * { [ { ( \frac{1+\sqrt{5}}{2} )}^k - { ( \frac{1-\sqrt{5}}{2} )}^k }] } $
= $ \sum^n_{k=0}{ C^k_n * {( \frac{1+\sqrt{5}}{2} )}^k } - \sum^n_{k=0}{ C^k_n * { ( \frac{1-\sqrt{5}}{2} )}^k } $
= $ { ( \frac{1+\sqrt{5}}{2} + 1 ) }^k $ - $ { ( \frac{1-\sqrt{5}}{2} + 1 ) }^k $
= $ { ( \frac{3+\sqrt{5}}{2} ) }^k $ - $ { ( \frac{3-\sqrt{5}}{2} ) }^k $
= $ { ( \frac{6+2*\sqrt{5}}{4} ) }^k $ - $ { ( \frac{6-2*\sqrt{5}}{4} ) }^k $
= $ { ( \frac{1+\sqrt{5}}{2} ) }^{2k} $ - $ { ( \frac{1-\sqrt{5}}{2} ) }^{2k} $
= $ f(2*k) $
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
int MOD;
struct Matrix{
LL a[][];
void init(int f){
memset(a,,sizeof a);
if(f==-)return;
for(int i=;i<;++i)a[i][i]=;
}
};
Matrix operator*(Matrix& A,Matrix& B){
Matrix C;C.init(-);
for(int i=;i<;++i)
for(int j=;j<;++j)
for(int k=;k<;++k){
C.a[i][j]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=MOD;
}
return C;
}
Matrix operator^(Matrix A,int n){
Matrix Rt;Rt.init();
for(;n;n>>=){
if(n&)Rt=Rt*A;
A=A*A;
}
return Rt;
}
int main(){
int n,Case;scanf("%d",&Case);
Matrix A,T;
T.a[][]=;T.a[][]=;
T.a[][]=;T.a[][]=;
for(;Case--;){
scanf("%d%d",&n,&MOD);
A=T^(n+n);
LL ans=A.a[][];
printf("%lld\n",ans%MOD);
} return ;
}
[HDU2855]Fibonacci Check-up的更多相关文章
- HDU2855—Fibonacci Check-up
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目意思:求一个式子g[n]=∑C(n,k)*f[k],n很大,很明显是一个矩阵快速幂.可以打表 ...
- 可变长度的Fibonacci数列
原题目: Write a recursive program that extends the range of the Fibonacci sequence. The Fibonacci sequ ...
- Applying Eigenvalues to the Fibonacci Problem
http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...
- hdu 5167 Fibonacci 打表
Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
- 【Fibonacci】BestCoder #28B Fibonacci
Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 5167 Fibonacci(预处理)
Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...
- [Algorithm] Fibonacci Sequence - Anatomy of recursion and space complexity analysis
For Fibonacci Sequence, the space complexity should be the O(logN), which is the height of tree. Che ...
- fibonacci数列的性质和实现方法
fibonacci数列的性质和实现方法 1.gcd(fib(n),fib(m))=fib(gcd(n,m)) 证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m ...
- LeetCode 842. Split Array into Fibonacci Sequence
原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...
随机推荐
- pandas 增删改查
原文链接:https://blog.csdn.net/zhangchuang601/article/details/79583551 准备工作:增.删.改.查的方法有很多很多种,这里只展示出常用的几种 ...
- Vagrant 手册之 Multi-machine 多机器
原文地址 Vagrant 可以在一个 Vagrantfile 中定义并控制多个虚拟机.这就是"multi-machine"环境. 这些机器可以协同工作或互相关联.multi-mac ...
- poj2431Expedition
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...
- MapReduce(2): How does Mapper work
In the previous post, we've illustrated how Hadoop MapReduce prepares input for Mappers. Long story ...
- [LeetCode] 137. Single Number II (位操作)
传送门 Description Given an array of integers, every element appears three times except for one, which ...
- eclipse配置,快捷键备忘
1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...
- .Net core 2.0 利用Attribute获取MVC Action来生成菜单
最近在学习.net core的同时将老师的MVC5项目中的模块搬过来用,其中有一块就是利用Attribute来生成菜单. 一·首先定义Action实体 /// <summary> /// ...
- oracle三大范式
范式: 设计数据库定义的一个规则, 三大范式, 灵活运用, 人的思想是活的 一范式 1, 不存在冗余数据 同一个表中的记录不能有重复----所以主键(必须有) 2, 每个字段必须是不可再分的信息(列不 ...
- array_map() 函数
定义和用法 array_map() 函数返回用户自定义函数作用后的数组.回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致. 语法 array_map(function,a ...
- 【五一qbxt】day6 OI中的stl
from:why 很多很多part…… 1.pair: 相当于把两个变量放在一起: #include<utility> using namespace std; pair<TypeN ...