矩阵快速幂基本应用。

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

如:在斐波那契数列之中

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. docker实战

    docker基础入门 docker网络

  2. DNA motif 搜索算法总结

    DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...

  3. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  4. dedecms操作数据库

    重要的事情说三遍:代码复制完要格式化,格式化,格式化,最好重新抄一遍,小心陷阱 一.配置数据库配置文件路径/data/common.inc.php二.连接数据库dirname(__FILE__)表示当 ...

  5. Laravel 本地化定义

    1.配置本地化语言Laravel 的本地化语言配置项位于config/app.php: [php] view plain copy 'locale' => 'zh',//当前语言 'fallba ...

  6. OSGi 系列(六)之服务的使用

    OSGi 系列(六)之服务的使用 1. 为什么使用服务 降低服务提供者和服务使用者直接的耦合,这样更容易重用组件 隐藏了服务的实现细节 支持多个服务的实现.这样你可以互换这实现 2. 服务的使用 2. ...

  7. Git使用1

    1.先配置本地Git E:\personal>git config –-global user.name "lewy" E:\personal>git config – ...

  8. linux tty设置详解

    http://blog.csdn.net/againyuan/article/details/3905380 linux串口termios NAME termios, tcgetattr, tcset ...

  9. 2018.10.17 NOIP模拟 管道(状压dp)

    传送门 状压dp好题. 怎么今天道道题都有点东西啊 对于今天题目神仙出题人先膜为上策:%%%%DzYoAk_UoI%%%% 设f[i][j]f[i][j]f[i][j]表示选取点的状态集合为iii,当 ...

  10. 2018.09.25 codeforces1053E. Euler tour(并查集+st表+模拟)

    传送门 毒瘤细节题. 首先考虑不合法的情况. 先把相同的值配对,这样就构成了一些区间. 那么如果这些区间有相交的话,就不合法了. 如何判断?DZYO安利了一波st表,我觉得很不错. 接着考虑两个相同的 ...