当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?
可以用矩阵快速幂来加速计算。
我们可以用矩阵来表示数列递推公式
比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n-1)    f(n-2)] [ 1 1 ]

                              [ 1 0 ] 
设A = [ 1 1 ]

    [ 1 0 ]

[f(n)   f(n-1)] = [f(n-2)   f(n-3)]*A*A
[f(n)   f(n-1)] = [f(2)   f(1)]*A^(n-2)
矩阵满足结合律,所以先计算A^(n-2),这个可以用一般快速二分幂的思想来计算。

BestCoder Round#8 1002
当n为奇数时,f(n) = 2 * f(n-1) + 1
当n为偶数时,f(n) = 2 * f(n-1)
将偶数项独立出来形成单独的一个数列 b(2*n) = 2 * b(2*n-1) + 1 = 4 * (2*n-2) + 2
即b(n) = 4 * b(n-1) + 2
当n为偶数时,计算b(n/2)即可
当n为奇数时,计算b(n/2) * 2 + 1即可
因为n很大,可以用矩阵快速幂来加速
递推矩阵为 [b(n)   2] = [b(n-1]   2] * [ 4 0 ]
                     [ 1 1 ]

 1 #include <stdio.h>
 2 #include <string.h>
 3 typedef long long LL;
 4 struct Matrix
 5 {
 6     LL matrix[2][2];
 7 };
 8 int n,m;
 9 Matrix operator *(const Matrix &lhs, const Matrix &rhs)
10 {
11     Matrix res;
12     memset(res.matrix, 0 ,sizeof(res.matrix));
13     int i,j,k;
14     for(k=0; k<2; ++k)
15         for(i=0; i<2; ++i)
16         {
17             if(lhs.matrix[i][k] == 0) continue;
18             for(j=0; j<2; ++j)
19             {
20                 if(rhs.matrix[k][j] == 0) continue;
21                 res.matrix[i][j] = (res.matrix[i][j] + lhs.matrix[i][k] * rhs.matrix[k][j]) % m;
22             }
23         }
24     return res;
25 }
26 Matrix operator ^(Matrix a, int k)
27 {
28     Matrix res;
29     int i,j;
30     for(i=0; i<2; ++i)
31         for(j=0; j<2; ++j)
32             res.matrix[i][j] = (i == j);
33     while(k)
34     {
35         if(k & 1)
36             res = res * a;
37         a = a * a;
38         k>>=1;
39     }
40     return res;
41 }
42
43 int main()
44 {
45     while(scanf("%d%d",&n,&m)!=EOF)
46     {
47         Matrix a;
48         a.matrix[0][0] = 4;
49         a.matrix[0][1] = 0;
50         a.matrix[1][0] = a.matrix[1][1] = 1;
51         int k = n / 2;
52         a = a ^ k;
53         LL ans =(2 * a.matrix[1][0]) % m;
54         if(n & 1 == 1)
55             ans = (ans * 2 + 1) % m;
56         printf("%lld\n",ans);
57
58
59     }
60     return 0;
61 }

View Code

转载于:https://www.cnblogs.com/justPassBy/p/3979457.html

矩阵快速幂---BestCoder Round#8 1002相关推荐

  1. Codeforeces Round #226 (Div. 2) E---Bear in the Field(矩阵快速幂)

    Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbere ...

  2. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 fn=c2∗n−6fn−1fn−2fn−3\begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{alig ...

  3. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

  4. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

  5. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  6. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  7. [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]

    解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...

  8. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

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

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

最新文章

  1. 语音识别:时间序列Damerau–Levenshtein距离
  2. bzoj 5090 组题
  3. 【CyberSecurityLearning 70】DC系列之DC-1渗透测试(Drupal)
  4. 获“CAIS紫金奖”,腾讯民汉翻译践行“科技向善”
  5. flutter scrollview_简单易上手的Flutter学习指南App,2020一起来玩转Flutter吧~
  6. 使用Spring Security添加RememberMe身份验证
  7. 【国际专场】laravel多用户平台(SaaS, 如淘宝多用户商城)的搭建策略
  8. 批改网禁止粘贴怎么破_重大利好!教育部声明,要求家长批改作业等行为,发现一起严处一起...
  9. SQL语句大全(转)
  10. Spring基于注解的方式二
  11. Confluence 6 匿名访问远程 API
  12. [转]如何才能在 IIS 7.5 使用 Windows PowerShell Snap-In 功能
  13. 使用Windows Performance Monitor进行SQL Server性能调整
  14. 《.Ne框架程序设计》随记(3)
  15. 解决Ubuntu下载缓慢问题
  16. 教你自己训练的pytorch模型转caffe(二)
  17. 声音“三要素”---响度(loudness),音高(pitch),音色(timbre)
  18. 华为日历怎么显示一月_华为手机日历怎么设置
  19. 【校招内推】字节跳动2022提前批开启
  20. Git学习————rm删除文件与文件找回

热门文章

  1. 人工智能,机器学习,深度学习入门好文,强烈推荐
  2. php 邮件类库,【php发送邮件类库】10个php发送邮件类库下载
  3. 2000坐标系高程与85高程转换_科普 | 如何在大疆智图中设置坐标系
  4. linux安装manjaro创建分区失败,如何安装Manjaro Linux [最强指南]? 看完就知道了
  5. linux杀dmol3进程,linux下运行Gaussian09进程被killed - 量子化学 - 小木虫 - 学术 科研 互动社区...
  6. 011_fastdfs-client-java模块
  7. mysql错误消息1_MySQL出错信息: Subquery returns more than 1 row及其解决方法
  8. keil添加hal库_使用Keil uVision5创建stm32 hal库项目过程
  9. jbod ugood 磁盘驱动状态_组成原理—磁盘/IO/中断
  10. python祝福祖国代码_国际文交所:9月17日-10月15日《祝福祖国信卡》《澳门爱与祝愿套票》《北京精神封》3个提货转仓公告...