题目链接:http://codeforces.com/problemset/problem/450/B

题意:

求这个的第n项。

题解:$f_{i+1} = f_i - f_{i-1} $

\begin{pmatrix} 1 & 1\\ -1 & 0 \end{pmatrix} *

\begin{pmatrix} f(n)& f(n-1) \end{pmatrix} =

\begin{pmatrix} f(n) - f(n-1) & f(n) \end{pmatrix}=

\begin{pmatrix} f(n+1) & f(n) \end{pmatrix}

代入前两项即可。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = ;
const ll mod = 1e9+; ll n,p; struct Matrix{
ll a[maxn][maxn];
void init(){
memset(a, , sizeof(a));
for(int i = ; i <= maxn;i++){
a[i][i] = ;
}
}
}; //矩阵乘法
Matrix mul(Matrix a, Matrix b){
Matrix ans;
for(int i = ;i <= ;++i){
for(int j = ;j <= ;++j){
ans.a[i][j] = ;
for(int k = ;k <= ;++k){
ans.a[i][j] = ans.a[i][j] % mod + a.a[i][k] * b.a[k][j] % mod;
ans.a[i][j] %= mod;
}
}
}
return ans;
} //矩阵快速幂
Matrix qpow(Matrix a,ll b){
Matrix ans;
ans.init();
while(b){
if(b & )
ans = mul(ans,a);
a = mul(a,a);
b >>= ;
}
return ans;
} void print(Matrix a){
for(int i = ; i <= n;++i){
for(int j = ;j <= n;++j){
cout << a.a[i][j]%mod<< " ";
}
cout << endl;
}
} int main(){
Matrix base;
Matrix ans;
int x,y,n;
cin>>x>>y>>n;
if(n == ){
cout<<(mod + x) % mod<<endl;
return ;
}
if( n == ){
cout<<(mod + y) % mod<<endl;
return ;
} ans.a[][] = y;ans.a[][] = x;
base.a[][] = ;base.a[][] = ;
base.a[][] = -;base.a[][] = ; ans = mul(ans,qpow(base,n-));
ll res = (mod + ans.a[][]) % mod;
cout<<res<<endl;
return ;
}

最新文章

  1. android自动获取短信验证码
  2. centos中crontab(计时器)用法详解
  3. 企业级搜索引擎Solr使用入门指南
  4. android 05 桢布局:FrameLayout 网格布据 GridLayout
  5. hibernate spring 事务配置
  6. 【开源java游戏框架libgdx专题】-10-核心库-Viewport
  7. IIS应用程序池监控
  8. jvisualvm远程监控Tomcat
  9. CKEditor 案例
  10. UNIX环境高级编程——进程间通信概念
  11. JVM笔记8-虚拟机性能监控与故障处理工具
  12. 版本管理工具Git(一)简要介绍
  13. 一文看懂大数据的技术生态Hadoop, hive,spark都有了[转]
  14. PreparedStatement 与 Statement 的区别
  15. kafka0.10
  16. 45、文件过滤器FilenameFilter
  17. HDU 2084(DP)
  18. SVN无法Cleanup
  19. recovery.conf文件详解
  20. Java 依赖注入标准(JSR-330)简介

热门文章

  1. 转 jmeter 等待时间 pacing think time
  2. proc伪文件系统 - 加载一个进程
  3. JS基础API
  4. 分分钟教你学会 ToolBar 的使用(转)
  5. day06 python is == 编码 解码
  6. HDU 6326 Problem H Monster Hunter
  7. 使用springBoot和mybatis整合时出现如下错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方案
  8. 【JVM】符号引用和直接引用
  9. Robot Framework:变量与运算
  10. 如何 修改jsp页面时间格式