当前位置:网站首页>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;
}边栏推荐
- [youcans' image processing learning course] general contents
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- JS generates 4-digit verification code
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- Daily question: 1175 Prime permutation
- Node.js通过ODBC访问PostgreSQL数据库
- Fundamentals of face recognition (facenet)
- D为何链接不了dll
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
猜你喜欢

(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software

Fundamentals of face recognition (facenet)

Daily practice of C language --- monkeys divide peaches

Can automatically update the universal weekly report template, you can use it with your hand!

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

de4000h存储安装配置

Answer: can the audio be set to on by default during easydss video on demand?

Unity SKFramework框架(十九)、POI 兴趣点/信息点

中文姓名提取(玩具代码——准头太小,权当玩闹)

Don't spend money, spend an hour to build your own blog website
随机推荐
Unity skframework framework (XXI), texture filter map resource filtering tool
Unity SKFramework框架(二十)、VFX Lab 特效库
mac(macos Monterey12.2 m1) 个人使用php开发
Answer: can the audio be set to on by default during easydss video on demand?
OpenAPI generator: simplify the restful API development process
JS generates 4-digit verification code
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Security RememberMe原理分析
OpenApi-Generator:简化RESTful API开发流程
Jerry's weather code table [chapter]
Jerry's watch modifies the alarm clock [chapter]
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Jerry's weather direction coding table [chapter]
D how to check null
Three methods of finding LCA of the nearest common ancestor
Essential for operation and maintenance - Elk log analysis system
What are eNB, EPC and PGW?