当前位置:网站首页>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;
}边栏推荐
- Download files and preview pictures
- 三谈exception——错误处理
- De4000h storage installation configuration
- Let juicefs help you with "remote backup"
- Web基础
- Jerry's weather code table [chapter]
- SSL证书的分类有哪些?如何选择合适的SSL证书?
- [unity] using GB2312, the solution to abnormal program after packaging
- Solve "sub number integer", "jump happily", "turn on the light"
- 2、 Frame mode MPLS operation
猜你喜欢

Engineers who can't read device manuals are not good cooks

Unity SKFramework框架(十二)、Score 计分模块

leetcode621. 任务调度器

Chinese name extraction (toy code - accurate head is too small, right to play)

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具

Node. JS accessing PostgreSQL database through ODBC

Essential for operation and maintenance - Elk log analysis system

二、帧模式 MPLS 操作
![Jerry's watch stops ringing [article]](/img/28/8a225b77b19360d2b0077a2b8c4b6d.jpg)
Jerry's watch stops ringing [article]

Daily practice of C language --- monkeys divide peaches
随机推荐
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Jerry's watch reads the alarm clock [chapter]
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
leetcode621. 任务调度器
Node. JS accessing PostgreSQL database through ODBC
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
记忆函数的性能优化
Fundamentals of machine learning (II) -- division of training set and test set
OpenFOAM:lduMatrix&lduAddressing
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
Web基础
口袋奇兵点评
Essential for operation and maintenance - Elk log analysis system
Android kotlin material design technology points
Student course selection information management system based on ssm+jsp framework [source code + database]
EasyDSS点播服务分享时间出错如何修改?
量子三体问题: Landau Fall
Jerry's weather code table [chapter]