当前位置:网站首页>Sum of the first n terms of Fibonacci (fast power of matrix)
Sum of the first n terms of Fibonacci (fast power of matrix)
2022-07-02 13:37:00 【AC__ dream】
analysis : From the properties of Fibonacci sequence, we can get the following recurrence formula , Using this recursive formula, we can use the fast power of the matrix to quickly find sn
The thing to notice is when n=1 Just give it a special judgment ,[fn-1,fn,sn]=[f1,f1,s2]* Recursive matrix n-2 Power , Then take out. sn The elements are , It will explode in the process int, So we have to drive longlong And in the process m modulus
Here is the code :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
int n,m;
void mul(ll ans[][4],ll a[][4],ll b[][4])
{
ll t[4][4]={0};
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
for(int k=1;k<=3;k++)
{
t[i][j]+=a[i][k]*b[k][j];
t[i][j]%=m;
}
memcpy(ans,t,sizeof t);
}
int main()
{
cin>>n>>m;
if(n==1) printf("%d",1%m);
else
{
ll t[4][4]={
{0,0,0,0},
{0,0,1,1},
{0,1,1,1},
{0,0,0,1}
};
ll ans[4][4]={
{0,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1}
};
n-=2;
while(n)
{
if(n&1) mul(ans,ans,t);
n>>=1;
mul(t,t,t);
}
printf("%lld",(ans[1][3]+ans[2][3]+2*ans[3][3])%m);
}
return 0;
}
边栏推荐
- Error function ERF
- 二、帧模式 MPLS 操作
- Security RememberMe原理分析
- net share
- Download files and preview pictures
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- 最近公共祖先LCA的三种求法
- Everyone wants to eat a broken buffet. It's almost cold
- Research shows that "congenial" is more likely to become friends
- 嵌入式软件开发
猜你喜欢
Student course selection information management system based on ssm+jsp framework [source code + database]
Engineers who can't read device manuals are not good cooks
Everyone wants to eat a broken buffet. It's almost cold
Web基础
Unity skframework framework (XIII), question module
记忆函数的性能优化
Error function ERF
日本赌国运:Web3.0 ,反正也不是第一次失败了!
嵌入式软件开发
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
随机推荐
Node. JS accessing PostgreSQL database through ODBC
科技的成就(二十七)
OpenFOAM:lduMatrix&lduAddressing
OpenApi-Generator:简化RESTful API开发流程
验证失败,请检查您的回电网址。您可以按照指导进行操作
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
Explanation: here is your UFO, Goldbach conjecture
Unity skframework framework (XV), singleton singleton
【youcans 的图像处理学习课】总目录
[indomitable medal activity] life goes on and writing goes on
Unity skframework framework (XIV), extension extension function
Redis数据库持久化
题解:《压缩技术》(原版、续集版)
Error function ERF
三谈exception——错误处理
Unity SKFramework框架(十三)、Question 问题模块
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Numpy array calculation
D how to check null
Nohup command