题目已经告诉你斐波那契矩阵算法了...

所以需要用到矩阵快速幂,看起来名字很diao但其实和普通快速幂基本一毛一样...

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <set>
 6 #include <stack>
 7 #include <math.h>
 8 #include <string>
 9 #include <algorithm>
10
11 #define SIGMA_SIZE 26
12 #define pii pair<int,int>
13 #define lson rt<<1
14 #define rson rt<<1|1
15 #define lowbit(x) (x&-x)
16 #define fode(i, a, b) for(int i=a; i>=b; i--)
17 #define foe(i, a, b) for(int i=a; i<=b; i++)
18 #define fod(i, a, b) for(int i=a; i>b; i--)
19 #define fo(i, a, b) for(int i=a; i<b; i++)
20 //#pragma warning ( disable : 4996 )
21
22 using namespace std;
23 typedef long long LL;
24 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
25 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
26 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
27 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
28 inline int Max(int a, int b) { return a>b ? a : b; }
29 inline int Min(int a, int b) { return a>b ? b : a; }
30 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
31 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
32 const LL INF = 0x3f3f3f3f3f3f3f3f;
33 const LL lmod = 1e9+7;
34 const int mod = 10000;
35 const double eps = 1e-8;
36 const int inf = 0x3f3f3f3f;
37 const int maxk = 1e5+5;
38 const int maxm = 510*510;
39 const int maxn = 2;
40
41 struct Matrix {
42     int mm[maxn][maxn];
43     Matrix() {}
44     Matrix operator*(Matrix const &b) const
45     {
46         Matrix tmp; memset(tmp.mm, 0, sizeof(tmp.mm));
47         fo(i, 0, maxn)
48             fo(j, 0, maxn)
49                 fo(k, 0, maxn)
50                     tmp.mm[i][j] = (tmp.mm[i][j] + this->mm[i][k]*b.mm[k][j])%mod;
51         return tmp;
52     }
53 };
54
55 Matrix qpow(Matrix base, int b)
56 {
57     Matrix res;
58     memset(res.mm, 0, sizeof(res.mm));
59     fo(i, 0, maxn) res.mm[i][i] = 1;
60     while(b)
61     {
62         if (b&1) res = res*base;
63         base = base*base;
64         b >>= 1;
65     }
66     return res;
67 }
68
69
70 int main()
71 {
72
73     #ifndef ONLINE_JUDGE
74         freopen("input.txt", "r", stdin);
75     #endif
76
77     int x;
78     while(cin >> x)
79     {
80         if (x==-1) break;
81         //if (x==0) cout << 0 << endl;
82
83         Matrix tmp, ans; memset(tmp.mm, 0, sizeof(tmp.mm));
84         tmp.mm[0][0] = tmp.mm[0][1] = tmp.mm[1][0] = 1;
85         tmp.mm[1][1] = 0;
86
87         ans = qpow(tmp, x);
88         printf("%d\n", ans.mm[0][1]);
89     }
90
91     return 0;
92 }

View Code

转载于:https://www.cnblogs.com/chaoswr/p/9780460.html

POJ - 3070 Fibonacci相关推荐

  1. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...

  2. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

  3. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  4. POJ 3070 Fibonacci

    裸奔的矩阵乘法,当模板了. #include <iostream>#include <cstring>#include <cstdio> using namespa ...

  5. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  6. poj - problem 3070 Fibonacci 【矩阵 +快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13732   Accepted: 9728 Descri ...

  7. POJ 3047 Fibonacci

    DEBUG很辛苦,且行, 且珍惜 原代码: ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000;an ...

  8. 一起开心2020暑假训练第一周

    hdu 1576 A/B oj传送 题解: Poj 1061 青蛙的约会 oj传送 题解: hdu 1525 Euclid's Game oj传送 题解: Poj 3070 Fibonacci oj传 ...

  9. 矩阵相关操作和矩阵快速幂

    矩阵相关操作和矩阵快速幂 矩阵基本运算以及快速幂模板 POJ - 3070. Fibonacci Hdu - 1757A. Simple Math Problem Codeforces - 185A. ...

最新文章

  1. Makefile中打印变量
  2. WildFly 8.0.0.Alpha1的发布和一些历史
  3. PHP创建圆柱体的类,创建一个类
  4. devops 解决了啥问题_您的DevOps有什么问题?
  5. java数组复制_Java自学-数组 复制数组
  6. 2017.8.26 力 思考记录
  7. 三种主流的Kubernetes部署方式
  8. C++学习(二六七)find_package() find_library()
  9. etree不能使用,etree.HTML()不能使用。
  10. 适合公司年会的4个热门互动小游戏
  11. 985硕士程序员年薪80万!邻居眼中不如一个老师?你怎么看?
  12. web攻击之一:XSS跨站脚本
  13. BJTU1935 铁憨憨骑士团的购买装备
  14. 关于数字的智力题-三个女儿的年龄
  15. 华为mate30是不是鸿蒙系统,这个意味着是不是鸿蒙系统?
  16. Python逐行读取tsv文件
  17. C++PrimerPlus
  18. 红帽linux怎么截图,Linux上使用Ksnip截图
  19. 【Python】不同目录下的.py文件调用问题
  20. 计算机的硬件和软件主成

热门文章

  1. 视图插入数据_带切片器的数据透视图
  2. faster rcnn第二阶段loss出现nan_利用Faster_Rcnn训练模型时出现的问题
  3. 网络营销推广解答网站排名上下摇摆不定怎么办?
  4. 网站转化率做不好多半是这三大因素产生干扰
  5. java demo在哪里下载_[Java教程]Java学习 (一)、下载,配置环境变量,第一个demo...
  6. python变量详解_Python 变量详解[学习 Python 必备基础知识][看此一篇就够了]
  7. vim 多窗口启动以及相互切换
  8. 开发日记-20190424 关键词 阶段性开发心得和小结
  9. 开发日记-20190407
  10. 安全学习概览——恶意软件分析、web渗透、漏洞利用和挖掘、内网渗透、IoT安全分析、区块链、黑灰产对抗...