当前位置:网站首页>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;
}边栏推荐
- ADB basic commands
- Nohup command
- Jerry's watch reads the alarm clock [chapter]
- JS reverse massive creative signature
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- 【OpenGL】笔记二十九、高级光照(镜面高光)
- What are eNB, EPC and PGW?
- 不会看器件手册的工程师不是个好厨子
- What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
- Jerry's weather direction coding table [chapter]
猜你喜欢

Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本

OpenFOAM:lduMatrix&lduAddressing

leetcode621. 任务调度器
![[cloud native database] what to do when encountering slow SQL (Part 1)?](/img/ac/a59189aba1901e769beec1ec7e6d32.png)
[cloud native database] what to do when encountering slow SQL (Part 1)?

What are eNB, EPC and PGW?

研究表明“气味相投”更易成为朋友

为什么switch 的default后面要跟break?

题解《子数整数》、《欢乐地跳》、《开灯》

MAC (MacOS Monterey 12.2 M1) personal use PHP development
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]
随机推荐
Jerry's watch modifies the alarm clock [chapter]
Redis database persistence
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Unity skframework framework (XIV), extension extension function
OpenFOAM:lduMatrix&lduAddressing
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
机器学习基础(二)——训练集和测试集的划分
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
JS逆向之行行查data解密
三谈exception——错误处理
Unity SKFramework框架(十九)、POI 兴趣点/信息点
MAC (MacOS Monterey 12.2 M1) personal use PHP development
Pocket Raider comments
能自动更新的万能周报模板,有手就会用!
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
P3807 [template] Lucas theorem /lucas theorem
D how to check null