当要求递推数列的第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(样品集成框架spring、spring mvc、spring data jpa、hibernate)

    这是你自己的参考springside集成框架的开源项目.主要的整合spring.spring mvc.spring data jpa.hibernate几个框架,对于这些框架中仍然感觉更舒适sprin ...

  2. 地大邀请赛d

    Problem D: Tetrahedron Inequality Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 15   Solved: 3 [ ...

  3. Indy的评价

    已经抛弃了indy,实在是不好,tcp在android下退出报错.我现在改用系统自带的httpclient.推荐RTC RTC带有一个tcp组件,不过处理方式跟indy不同,测试过,在android下 ...

  4. blender资源

    blender资源 只能发帖不能更改的百度贴吧贴. http://tieba.baidu.com/f?kw=blender blendercn youku视频专辑: http://i.youku.co ...

  5. Java回调理解 (step by step)

    在网上搜索了很多篇关于java回调函数的文章,自己也来试了一下写了这篇博客,可能有些地方理解不到位,烦请各位大大指正. 在计算机程序设计中.回调函数.或简称回调.是指通过函数參数传递到其他代码的,某一 ...

  6. TCP连接的建立(二)

    被动打开 SYN cookies TCP协议开辟了一个比較大的内存空间请求连接队列来存储连接请求块,当SYN请求不断添加,请求连接数目到达上限时,会致使系统丢弃SYN连接请求.SYN cookies技 ...

  7. Matlab实现Hough变换检測图像中的直线

    Hough变换的原理: 将图像从图像空间变换至參数空间.变换公式例如以下: 变换以后,图像空间与參数空间存在下面关系: 图像空间中的一点在參数空间是一条曲线,而图像空间共线的各点相应于參数空间交于一点 ...

  8. 30分钟快速掌握AngularJs

    [后端人员耍前端系列]AngularJs篇:30分钟快速掌握AngularJs   一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来 ...

  9. java学习笔记13--比较器(Comparable、Comparator)

    java学习笔记13--比较器(Comparable.Comparator) 分类: JAVA 2013-05-20 23:20 3296人阅读 评论(0) 收藏 举报 Comparable接口的作用 ...

  10. 7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml

     数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDef ...