当要求递推数列的第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 ]

 #include <stdio.h>
#include <string.h>
typedef long long LL;
struct Matrix
{
LL matrix[][];
};
int n,m;
Matrix operator *(const Matrix &lhs, const Matrix &rhs)
{
Matrix res;
memset(res.matrix, ,sizeof(res.matrix));
int i,j,k;
for(k=; k<; ++k)
for(i=; i<; ++i)
{
if(lhs.matrix[i][k] == ) continue;
for(j=; j<; ++j)
{
if(rhs.matrix[k][j] == ) continue;
res.matrix[i][j] = (res.matrix[i][j] + lhs.matrix[i][k] * rhs.matrix[k][j]) % m;
}
}
return res;
}
Matrix operator ^(Matrix a, int k)
{
Matrix res;
int i,j;
for(i=; i<; ++i)
for(j=; j<; ++j)
res.matrix[i][j] = (i == j);
while(k)
{
if(k & )
res = res * a;
a = a * a;
k>>=;
}
return res;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
Matrix a;
a.matrix[][] = ;
a.matrix[][] = ;
a.matrix[][] = a.matrix[][] = ;
int k = n / ;
a = a ^ k;
LL ans =( * a.matrix[][]) % m;
if(n & == )
ans = (ans * + ) % m;
printf("%lld\n",ans); }
return ;
}

矩阵快速幂---BestCoder Round#8 1002的更多相关文章

  1. 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E

    http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...

  2. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  3. hdu 5607 BestCoder Round #68 (矩阵快速幂)

    graph  Accepts: 9 Submissions: 61  Time Limit: 8000/4000 MS (Java/Others)  Memory Limit: 65536/65536 ...

  4. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】

    #include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  8. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  9. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

随机推荐

  1. java面向对象下:Java数据库编程

    19.Java数据库编程: JDBC概述:        JDBC(Java Database Connection)是java中提供的一套数据库编程API,它定义了一套用来访问数据库的标准Java类 ...

  2. 【Android UI设计与开发】第17期:滑动菜单栏(二)开源项目SlidingMenu的示例

    通过上一篇文章的讲解,相信大家对于开源项目SlidingMenu都有了一个比较初步的了解(不了解的可以参考上 一篇文章),那么从这一章开始,博主将会以SlidingMenu为重心,给大家带来非常丰富的 ...

  3. android面试题 不仅仅是面试是一个很好的学习

    下面的问题是在网上找到的总结,感谢您分享!希望,我们的共同进步,找到自己心仪的公司,: 1.android dvm 流程和Linux这个过程.无论是应用程序对同一概念: 答案:dvm是dalivk虚拟 ...

  4. SVN的项目管理

    基于SVN的项目管理——集中与分散   我们在此处不讨论 GIT 比 SVN 好多少,也不讨论 Maven 和 Gradle 哪个好用,基于现有的开发环境,大多数公司还是采用 SVN + Maven ...

  5. 14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚

    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚 如果自动提交模式被启用,在InnoDB里, 所有的用户活动发生在一个事务里, 每个SQL语句 ...

  6. HTTP POST请求的Apache Rewrite规则设置

    最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为:     ...

  7. c语言实现动态指针数组Dynamic arrays

    c语言实现动态数组.其它c的数据结构实现,hashTable參考点击打开链接 treeStruct參考点击打开链接 基本原理:事先准备好一个固定长度的数组. 假设长度不够的时候.realloc一块区域 ...

  8. BPL vs. DLL

    第一部分:有关包的介绍 一般我们编写编译一个DELPHI应用程序时,会产生一个EXE文件,也就是一个独立的WINDOWS应用程序.很重要的一点:区别于Visual Basic,DELPHI产生的是预先 ...

  9. easy_install MySQL-python

    python - Why can't easy_install find MySQLdb? - Stack Overflow easy_install MySQL-python

  10. iOS中的字符串NSString

    创建一个字符串对象: NSstring * str1 = @"hello world"; NSString * str = [[NSString alloc]initWithStr ...