HDU2855—Fibonacci Check-up
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855
题目意思:求一个式子g[n]=∑C(n,k)*f[k],n很大,很明显是一个矩阵快速幂。可以打表发现g[n]=f[2*n]划开可以发现g[n]=3*g[n-1]-f[n-2]。
思路:我们现在可以证明一下
f[2*n]=f[2*n-1]+f[2*n-2];
其中f[2*n-1]=f[2*n-2]+f[2*n-3]
f[2*n-2]=f[2*n-3]+f[2*n-4]
所以f[2*n]=f[2*n-2]+f[2*n-3]+f[2*n-3]+f[2*n-4]=2*f[2*n-2]+f[2*n-3]=2*f[2*n-2]+f[2*n-3]+f[2*n-4]-f[2*n-4]=3*f[2*n-2]-f[2*n-4]。我们化简一下g[n]=3*g[n-1]-g[n-2]。我们可以通过打表得到这个结论。
现在我们可以从数学上证明一下这个结论。
方法:1.通项公式:f[n]=(1/sqrt(5))*(((1+sqrt(5))/2)^n-((1-sqrt(5))/2)^n)
2.二项式:(1+a)^n=∑(C(n,k)*a^k)(0<=k<=n)
∑C(n,k)*f[k]=(1/sqrt(5))*∑C(n,k)*(((1+sqrt(5))/2)^k-((1-sqrt(5))/2)^k)
=(1/sqrt(5))*(∑C(n,k)*((1+sqrt(5))/2)^k-∑C(n,k)*((1-sqrt(5))/2)^k)
=(1/sqrt(5))*((3+sqrt(5))/2)^n-((3-sqrt(5))/2)^n)
=(1/sqrt(5))*((1+sqrt(5))/2)^(2*n)-((1-sqrt(5))/2)^(2*n))
=f[2*n]
这个组合数的应用感觉十分有意思。
代码:
//Author: xiaowuga
#include<bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define N 2
using namespace std;
long long MOD;
typedef long long ll;
ll n,size=;//第n项,矩阵大小
struct Matrix{
ll mat[N][N];
void clear(){
memset(mat,,sizeof(mat));
}
Matrix operator * (const Matrix & m) const{
Matrix tmp;
int i ,j,k;
tmp.clear();
for( i=;i<size;i++)
for( k=;k<size;k++){
if(mat[i][k]==) continue;
for( j=;j<size;j++){
tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
tmp.mat[i][j]%=MOD;
}
}
return tmp;
}
};
Matrix POW(Matrix m,ll k){
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for(int i=;i<size;i++) ans.mat[i][i]=;
while(k){
if(k&) ans=ans*m;
k/=;
m=m*m;
}
return ans;
}
int main() {
Matrix m;
m.clear();
m.mat[][]=;m.mat[][]=-;
m.mat[][]=;m.mat[][]=;
int T;
cin>>T;
for(int i=;i<T;i++){
cin>>n>>MOD;
if(n==) {cout<<<<endl;continue;}
Matrix ans=POW(m,n-);
ll sum=(ans.mat[][]%MOD+MOD)%MOD;
cout<<sum<<endl;
}
return ;
}
HDU2855—Fibonacci Check-up的更多相关文章
- [HDU2855]Fibonacci Check-up
题目:Fibonacci Check-up 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 分析: 1)二项式展开:$(x+1)^n = \sum^ ...
- 可变长度的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 ...
随机推荐
- python3.7+opencv3.4.1
https://solarianprogrammer.com/2016/09/17/install-opencv-3-with-python-3-on-windows/ https://www.cnb ...
- css语法和JS语法的对比
CSS语法(不区分大小写) JavaScript语法(区分大小写) border border border-bottom borderBottom border-bottom-color bor ...
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- wget镜像网站并且下载到指定目录 2012-06-20 19:40:56
wget镜像网站并且下载到指定目录 2012-06-20 19:40:56 分类: Python/Ruby wget -r -p -np -k -P /tmp/ap http://www.exampl ...
- angularJs 页面定时刷新
angularJs 页面定时刷新 页面定时刷新并在页面离开时停止自动刷新 var autoRefresh; //自动刷新 autoRefresh = $interval($scope.loadData ...
- Git代码仓库的建立流程
Git作为现在比较流行的版本管理工具,其配置非常简单.方便. 下面举一个简单例子,说明如何在服务器上建立一个公共的git代码仓库. 1.确保服务器上已经打开ssh服务,可以用ps -e | grep ...
- beaglebone black ubuntu display x11 server的配置
Change default resolution on BeagleBone modesetting vs fbdev digiteltlc May 7th, 2014, 03:28 PM Hi ...
- VSS (Visual Source Safe 2005) 用法详解
VSS用法指南 The usage of VSS (Visual Source Safe 2005) 1. 首先,当然是得安装好Visual Source Safe 2005 你可以在Visual S ...
- 我们把Mybatis的功能架构分为三层:
我们把Mybatis的功能架构分为三层: (1)API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库.接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理. (2) ...
- XML 是一种元语言, 可以用它来描述其他语言。
A.正确 B.错误 解答:B XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup L ...