当要求递推数列的第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. 求刷Kindle Fire HD的方法

    前几天入手了台Amazon Kindle Fire HD 其系统是经过Amazon尝试改造过的Android,用起来很不爽,想刷个CM10之类的,求教程和工具.

  2. Spring Security Source Code -- 验证标准流程

    除了初始阶段: 主干验证流程链: MyInvocationSecurityMetadataSource.getAttributes(Object) line: 43     MyFilterSecur ...

  3. 与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态

    原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态 [索引页][源码下载] 与众不同 windows phone (23) - Devic ...

  4. Mysql InnoDB 是IOT表 锁基于索引

    </pre>Mysql InnoDB 是IOT表 锁基于索引<pre>

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

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

  6. TP-LINK无线路由器WR340G+ 54M支持WDS - 东莞市泰讯电子科技有限公司

    TP-LINK无线路由器WR340G+ 54M支持WDS - 东莞市泰讯电子科技有限公司 TP-LINK无线路由器WR340G+ 54M支持WDS 品牌  TP-LINK无线路由器 型号  WR340 ...

  7. 【IOS实例小计】图像移动--可扩展为动态实现图标变化

    预备知识: 1.页面切换: 从一个ViewController切换到另一个ViewController有下面几种方法: (1)self.view addSubview:(加载的新页面);      相 ...

  8. 字符串转换为整数”123“-&gt;123

    字符串转换为整数"123"->123 题目描写叙述: 输入一个由数字组成的字符串.把它转换成整数并输出. 比如:输入字符串"123".输出整数123. 给 ...

  9. data URI scheme及其应用

    data URI scheme通俗的来讲就是将一张图片直接塞到HTML中而不是通过HTTP请求去获取.这样从表面上看会降低一次HTTP的请求,实现了对于网页的优化(只是看了其它一些文章data URI ...

  10. hdu 1070Milk

    地址链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:多看几遍,学着静下来心去看英文题 代码: #include<bits/stdc++. ...