[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 ...
随机推荐
- Spark SQL实战
一.程序 package sparklearning import org.apache.log4j.Logger import org.apache.spark.SparkConf import o ...
- 网络设备MIB浏览器ifType、ifDescr、ifMtu、ifInOctets等的含义(Zabbix SNMP)
1.ifType 接口的类型 取值117表示接口为GigabitEthernet (取值62表示接口为 FastEnthernet) 2.ifDescr 接口类型的描述 有GigabitEtherne ...
- MySQL 查询语句--------------进阶9:联合查询
#进阶9:联合查询 /* union 联合 合并:将多条查询语句的结果合并成一个结果 语法: 查询语句1 union 查询语句2 union..... 应用场景:要查询的结果来自于多个表,且多个表没有 ...
- HTML DOM cursor 属性
值 描述 url 需被使用的自定义光标的URL 注释:请在此列表的末端始终定义一种普通的光标,以防没有由 URL 定义的可用光标. default 默认光标(通常是一个箭头) auto 默认.浏览器设 ...
- JNDI配置笔记
先在tomcat Context.xml配置文件中配置 <Resource name="jdbc/elifecrm" type="javax.sql.DataSou ...
- Pandas matplotlib 无法显示中文
Pandas 无法显示中文问题 解决方案 Pandas在绘图时,会显示中文为方块,主要原因有二: matplotlib 字体问题seaborn 字体问题 (实际上,matplotlib是支持uni ...
- Django读写分离
多数据库配置 数据库配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join ...
- C Sleepy Kaguya
链接:https://ac.nowcoder.com/acm/contest/338/C来源:牛客网 题目描述 Houraisan☆Kaguya is the princess who lives i ...
- 【java】jstack分析查看线程状态
演示代码 public class StackTest { public static void main(String[] args) { Thread thread = new Thread(ne ...
- CSRF——跨站请求伪造
一.CSRF是什么CSRF,全称:Corss-site request forgery,中文名称:跨站请求伪造.CSRF攻击比XSS攻击更具危险性,被安全界称为“沉睡的巨人”. 二.CSRF可以做什么 ...