【Codeforces】450 B(div2)
2024-09-03 20:08:34
题目链接: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 ;
}
最新文章
- android自动获取短信验证码
- centos中crontab(计时器)用法详解
- 企业级搜索引擎Solr使用入门指南
- android 05 桢布局:FrameLayout 网格布据 GridLayout
- hibernate spring 事务配置
- 【开源java游戏框架libgdx专题】-10-核心库-Viewport
- IIS应用程序池监控
- jvisualvm远程监控Tomcat
- CKEditor 案例
- UNIX环境高级编程——进程间通信概念
- JVM笔记8-虚拟机性能监控与故障处理工具
- 版本管理工具Git(一)简要介绍
- 一文看懂大数据的技术生态Hadoop, hive,spark都有了[转]
- PreparedStatement 与 Statement 的区别
- kafka0.10
- 45、文件过滤器FilenameFilter
- HDU 2084(DP)
- SVN无法Cleanup
- recovery.conf文件详解
- Java 依赖注入标准(JSR-330)简介
热门文章
- 转 jmeter 等待时间 pacing think time
- proc伪文件系统 - 加载一个进程
- JS基础API
- 分分钟教你学会 ToolBar 的使用(转)
- day06 python is == 编码 解码
- HDU 6326 Problem H Monster Hunter
- 使用springBoot和mybatis整合时出现如下错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方案
- 【JVM】符号引用和直接引用
- Robot Framework:变量与运算
- 如何 修改jsp页面时间格式