矩阵快速幂基本应用。

对于矩阵乘法与递推式之间的关系:

如:在斐波那契数列之中

f[i] = 1*f[i-1]+1*f[i-2]  f[i-1] = 1*f[i-1] + 0*f[i-2]。即

所以,

就这两幅图完美诠释了斐波那契数列如何用矩阵来实现。

 #include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int Mod=;
struct mat{
ll a[][];
};
mat mat_mul(mat x,mat y){
mat ans;
memset(ans.a,,sizeof(ans.a));
for (int i=;i<;i++){
for (int j=;j<;j++)
for (int k=;k<;k++)
ans.a[i][j]=ans.a[i][j]+(x.a[i][k]*y.a[k][j])%Mod;
}
return ans;
}
ll mat_pow(ll n){
mat c,res;
c.a[][]=c.a[][]=c.a[][]=;
c.a[][]=;
memset(res.a,,sizeof(res.a));
for (int i=;i<;i++) res.a[i][i]=;
while (n){
if (n&) res=mat_mul(res,c);
c=mat_mul(c,c);
n>>=;
}
return res.a[][]%Mod;
}
int main(){
ll n;
while (cin >> n && n!=-){
cout << mat_pow(n) << endl;
}
return ;
}

poj3070 Fibonacci(矩阵快速幂)的更多相关文章

  1. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  2. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  3. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  4. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  5. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  6. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  7. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  8. $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

    正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...

  9. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  10. hdu 3306 Another kind of Fibonacci 矩阵快速幂

    参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...

随机推荐

  1. 向Ubuntu的Dash中添加图标

    首先准备.xpm图标文件,如果程序文件夹中没有,那么可以根据自己喜好到网上下载喜欢的图标,不要太大,然后将其改为.xpm文件(直接改了后缀名就行).然后打开/usr/share/application ...

  2. ubuntu下常用操作

    屏幕截图: 可以用ubuntu自带的截图软件:gnome-screenshot. 该工具截图区域并且复制到剪切板命令为  gnome-screenshot -c -a,可以给该命令添加快捷方式,alt ...

  3. UI设计,你为什么不能把标题做的更明显呢?

    在设计中标题常常被重视,标题即是文案信息的精华提炼,那么如何能把标题在很多文案信息中脱颖而出就是设计师所要做的工作,前面的文章说过对比可以凸显主题,这期是在对比合理的前提下更进一步的处理方法,我们可以 ...

  4. Perl注释文本的高亮显示规则

    sub help{ print <<EndOfUsage;\e[1;37mHELP :1. Usage :  perl $0 input output 2. Function : tran ...

  5. Mac下PHP+MySQL+Apache2环境搭建

    本机系统信息如下: -------------------------------------------------------------------------------------- OS: ...

  6. 27. Green Building 绿色建筑

    27. Green Building 绿色建筑 ①When we think of green buildings,we tend to think of new ones-the kind of h ...

  7. MySQL 的IFNULL()、ISNULL()和NULLIF()函数

    参考与http://blog.csdn.net/xingyu0806/article/details/52080962 IFNULL(expr1,expr2) 假如expr1不为NULL,则 IFNU ...

  8. HDU 3361 ASCII (水题,不说什么了)

    题意:给你n个十进制数,让你输出相应的ASCII. 析:无,没说的,直接输出就好了. 代码如下: #include <iostream> #include <cstdio> # ...

  9. Navicat如何导出数据库的svg、pdf,png图片

    有时候各位可能有这么一种感觉,如果一个数据库中的表太多的话,查看起来不大方便,如果你习惯用navicat软件来查看er图的话,那也是更困难了,这里介绍一种方法,就是把这些关系结构导出一个可以用浏览器打 ...

  10. WPF MediaKit的一点问题

    原版WPF MediaKit在捕获摄像头视频时,如果不使用640*480分分辨率输出,会出现NewVideoSample事件不被触发的问题. 经数日摸索,终于明白SetVideoCapturePara ...