1732 Fibonacci数列 2
2024-10-21 14:25:11
题目描述 Description
在“1250 Fibonacci数列”中,我们求出了第n个Fibonacci数列的值。但是1250中,n<=109。现在,你的任务仍然是求出第n个Fibonacci数列的值,但是注意:n为整数,且1 <= n <= 100000000000000
输入描述 Input Description
输入有多组数据,每组数据占一行,为一个整数n(1 <= n <= 100000000000000)
输出描述 Output Description
输出若干行。每行输出第(对应的输入的)n个Fibonacci数(考虑到数会很大,mod 1000000007)
样例输入 Sample Input
3
4
5
样例输出 Sample Output
2
3
5
数据范围及提示 Data Size & Hint
1 <= n <= 100000000000000
分类标签 Tags 点此展开
2017-03-24
#include<cstdio>
#include<cstring>
#define m(s) memset(s,0,sizeof s)
using namespace std;
typedef long long ll;
const ll mod=1e9+;ll n;
struct matrix{ll s[][];}A,F;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=;i<;i++){
for(int j=;j<;j++){
c.s[i][j]=;
for(int k=;k<;k++){
c.s[i][j]+=a.s[i][k]*b.s[k][j];
c.s[i][j]%=mod;
}
}
}
return c;
}
//Fi=A^i*F0;
matrix fpow(matrix a,ll p){
matrix ans;
for(int i=;i<;i++)for(int j=;j<;j++) ans.s[i][j]=(i==j);
for(;p;p>>=,a=a*a) if(p&) ans=ans*a;
return ans;
}
int main(){
while(scanf("%lld",&n)==){
F.s[][]=;F.s[][]=F.s[][]=F.s[][]=;
A.s[][]=A.s[][]=A.s[][]=;A.s[][]=;
if(n>) F=fpow(A,n-)*F;
printf("%lld\n",F.s[][]);
}
return ;
}
2016-09-17
AC代码:
#include<cstdio>
#include<cstring>
#define ll long long
#ifdef unix
#define LL "%lld"
#else
#define LL "%I64d"
#endif
using namespace std;
const ll mod=1e9+;
struct node{
ll a[][];
}ans,ss;
ll n;
inline node mul(node &a,node &b){
node c;
for(int i=;i<;i++){
for(int j=;j<;j++){
c.a[i][j]=;
for(int k=;k<;k++){
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
}
}
}
return c;
}
void fpow(ll p){
for(;p;p>>=,ss=mul(ss,ss)) if(p&) ans=mul(ans,ss);
}
int main(){
while(scanf(LL,&n)==){
if(n==){puts("");continue;}
if(n==){puts("");continue;}
if(n==){puts("");continue;}
ans.a[][]=ans.a[][]=;ans.a[][]=ans.a[][]=;
ss.a[][]=ss.a[][]=ss.a[][]=;ss.a[][]=;
n--;
fpow(n);
printf(LL,ans.a[][]);putchar('\n');
}
return ;
}
最新文章
- ubuntu下配置jdk
- 佳能6d 魔灯
- PS:蓝天白云的制作
- PowerShell控制台快捷键
- OpenJudge计算概论-简单算术表达式求值
- java数据结构和算法------插入排序
- bzoj3237
- Javascript 对输入框中的内容进行 “全选/反选”
- python基础教程笔记—画幅好画(详解)
- Ipython的安装/ipython notebook的简单使用
- Spark 学习笔记
- 阻塞队列(BlockingQueue)
- STL vector简单用法
- Vue的路由动态重定向和导航守卫
- C语言程序设计基础知识点概括
- C#耗时计算
- 用Collections升降排序
- 【Hive】数据类型
- LightOJ 1038 Race to 1 Again (概率DP,记忆化搜索)
- 【MATLAB 从零到进阶】day2 矩阵 数组