P1962 斐波那契数列
题目背景
大家都知道,斐波那契数列是满足如下性质的一个数列:
• f(1) = 1
• f(2) = 1
• f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数)
题目描述
请你求出 f(n) mod 1000000007 的值。
输入输出格式
输入格式:
·第 1 行:一个整数 n
输出格式:
第 1 行: f(n) mod 1000000007 的值
题解
/*
落谷:p1962
题意:求斐波那契数列f(n)mod1e9+7 n在long long范围内
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL n,p;
struct node
{
LL m[3][3];
node(){
memset(m,0,sizeof(m));
}
friend node operator *(node x,node y){
node t;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
t.m[i][j]=0;
}
}
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
//尽量这样mod规范些
t.m[i][j]+=((x.m[i][k]%mod)*(y.m[k][j]%mod))%mod;
}
}
}
return t;
}
}a,ans;
node binary(node x,LL y)
{
while(y){
if(y&1) ans=ans*x;
x=x*x;
y>>=1;
}
return ans;
}
void Init()
{
a.m[1][1]