1732 Fibonacci数列 2

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 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 ;
}

最新文章

  1. ubuntu下配置jdk
  2. 佳能6d 魔灯
  3. PS:蓝天白云的制作
  4. PowerShell控制台快捷键
  5. OpenJudge计算概论-简单算术表达式求值
  6. java数据结构和算法------插入排序
  7. bzoj3237
  8. Javascript 对输入框中的内容进行 “全选/反选”
  9. python基础教程笔记—画幅好画(详解)
  10. Ipython的安装/ipython notebook的简单使用
  11. Spark 学习笔记
  12. 阻塞队列(BlockingQueue)
  13. STL vector简单用法
  14. Vue的路由动态重定向和导航守卫
  15. C语言程序设计基础知识点概括
  16. C#耗时计算
  17. 用Collections升降排序
  18. 【Hive】数据类型
  19. LightOJ 1038 Race to 1 Again (概率DP,记忆化搜索)
  20. 【MATLAB 从零到进阶】day2 矩阵 数组

热门文章

  1. Xcode相关概念:Target、Project、Scheme、Workspace
  2. GetArxPath
  3. CAD二次开发控件,dwg控件,网页DWG控件,手机浏览编辑DWG控件
  4. Redis系列(四)--持久化
  5. 05C语言数组
  6. CentOS 6 Yum本地源配置
  7. 【转】Flex 布局
  8. Flask蓝图基本使用
  9. 【Codeforces 372A】Counting Kangaroos is Fun
  10. JavaScript学习总结(12)——2016 年 7 个顶级 JavaScript 框架