题目: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的更多相关文章

  1. HDU2855—Fibonacci Check-up

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目意思:求一个式子g[n]=∑C(n,k)*f[k],n很大,很明显是一个矩阵快速幂.可以打表 ...

  2. 可变长度的Fibonacci数列

    原题目: Write a recursive program that extends the range of the Fibonacci sequence.  The Fibonacci sequ ...

  3. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  4. hdu 5167 Fibonacci 打表

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  5. 【Fibonacci】BestCoder #28B Fibonacci

    Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. hdu 5167 Fibonacci(预处理)

    Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...

  7. [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 ...

  8. fibonacci数列的性质和实现方法

    fibonacci数列的性质和实现方法 1.gcd(fib(n),fib(m))=fib(gcd(n,m)) 证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m ...

  9. LeetCode 842. Split Array into Fibonacci Sequence

    原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...

随机推荐

  1. 【SD系列】SAP SD模块-创建供应商主数据BAPI

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD模块-创建供应商主数据B ...

  2. HDU 1269 迷宫城堡 (Kosaraju)

    题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...

  3. Web控件中Eval()的使用

    1.使用Eval()绑定数据时使用三元运算符 <%#Eval("hg_A").ToString()=="1"?"通过":Eval(&q ...

  4. EasyUI在子tab基础上再打开新的tab标签页

    var title = "xxxx"; var content = '<iframe scrolling="auto" frameborder=" ...

  5. mysql 分区与性能

    数据库应用分为两类: OLTP(在线事务处理):如bolg.电子商务.网络游戏等 OLAP(在线分析处理):如数据仓库.数据集市 对于OLAP,分区可以很好的提高查询性能,应用大多数据需要频繁地扫描一 ...

  6. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)

    题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...

  7. 简单写入excel

    import pymysql,xlwt def to_excel(table_name): host, user, passwd, db = '127.0.0.1', 'root', '123', ' ...

  8. poj Meteor Shower

    这道题是下流星,流星会下到上下左右中的位置,而且有时间的,要你求出最短到达安全位置的时间. 这道题要注意边界是可以超过300的 #include<stdio.h> #include< ...

  9. 标签的增加、删除与复制,动态标签js不生效的解决

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. sql server 自优化

    大数据量下的SQL Server数据库自身优化 发布时间:2013-12-17 15:19:00 来源:论坛 作者:佚名   关键字:数据库开发 1.1:增加次数据文件 从SQL SERVER 200 ...