2017-09-13 19:22:01

writer:pprp

题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数

.

这是原理,实现部分就是矩阵的快速幂,也就是二分来做

矩阵快速幂可以用来解决线性递推方程,难点在于矩阵的构造

代码如下:

/*
@theme:用矩阵快速幂解决线性递推公式-斐波那契数列
@writer:pprp
@begin:21:17
@end:19:10
@error:注意mod的位置,不能连用,要加括号来用
@date:2017/9/13
*/ #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long ll;
const int mod=; struct Mat
{
ll a[][];
}; Mat mat_mul(Mat x, Mat y)
{
Mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
res.a[i][j] += x.a[i][k] * y.a[k][j];
res.a[i][j] %= mod;
}
return res;
} void quick_pow(ll n)
{
Mat E,res;
E.a[][] = E.a[][] = E.a[][] = ;
E.a[][] = ;
memset(res.a,,sizeof(res.a)); for(int i = ; i < ; i++)//二阶单位矩阵
res.a[i][i] = ; while(n)
{
if(n&)
res = mat_mul(res,E);
E = mat_mul(E,E);
n >>= ;
}
cout << res.a[][] << endl;
} int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin >> n && n != -)
{
quick_pow(n);
}
return ;
}
/*
@theme:用矩阵快速幂解决线性递推公式-斐波那契数列
@writer:pprp
@begin:21:17
@end:19:10
@error:注意mod的位置,不能连用,要加括号来用
@date:2017/9/13
*/ #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long ll;
const int mod=; struct Mat
{
ll a[][];
}; Mat mat_mul(Mat x, Mat y)
{
Mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
res.a[i][j] += x.a[i][k] * y.a[k][j];
res.a[i][j] %= mod;
}
return res;
} void quick_pow(ll n)
{
Mat E,res;
E.a[][] = E.a[][] = E.a[][] = ;
E.a[][] = ;
memset(res.a,,sizeof(res.a)); for(int i = ; i < ; i++)//二阶单位矩阵
res.a[i][i] = ; while(n)
{
if(n&)
res = mat_mul(res,E);
E = mat_mul(E,E);
n >>= ;
}
cout << res.a[][] << endl;
} int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin >> n && n != -)
{
quick_pow(n);
}
return ;
}

解题报告:poj 3070 - 矩阵快速幂简单应用的更多相关文章

  1. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...

  2. POJ 3070 矩阵快速幂

    题意:求菲波那切数列的第n项. 分析:矩阵快速幂. 右边的矩阵为a0 ,a1,,, 然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列. #include <c ...

  3. poj 3070 矩阵快速幂模板

    题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...

  4. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  5. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  6. poj 3734 矩阵快速幂+YY

    题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...

  7. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  8. poj 3744 矩阵快速幂+概率dp

    题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...

  9. Blocks(POJ 3734 矩阵快速幂)

    Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...

随机推荐

  1. Spark源码分析 – SparkEnv

    SparkEnv在两个地方会被创建, 由于SparkEnv中包含了很多重要的模块, 比如BlockManager, 所以SparkEnv很重要 Driver端, 在SparkContext初始化的时候 ...

  2. EIT: where is it now and what lies ahead?

    EIT: where is it now and what lies ahead? Electrical impedance tomography (EIT) is an emerging clini ...

  3. golang函数学习笔记

    golang函数特点: a.不支持重载,一个包不能有两个名字一样的函数 b.函数是一等公民,函数也是一种类型,一个函数可以赋值给变量 c.匿名函数 d.多返回值   例子1 func add(a, b ...

  4. 【react 样式】给react组件指定style

    1.使用行内样式(优先级高) 自定义的react组件是没有style属性的,如果要给想给自定义react组件指定style,我的方法是用一个<div>包裹自定义组件,然后给div指定sty ...

  5. HttpRunnerManager平台异步生成及展示代码覆盖率报告

    ant+jacoco+jenkins+HttpRunnerManager代码覆盖率统计平台搭建 实现思路通过jenkins构建,并使用HttpRunnerManager异步实现报告更新与展示. 现在整 ...

  6. js 屏蔽浏览器右键菜单

    <script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...

  7. jenkins SSH登录 Git配置(通过eclipse生成SSH 密钥)

    1.通过eclipse生成SSH 密钥 菜单栏的windows-->preferences-->General-->Network Connections-->SSH2--&g ...

  8. spark学习(基础篇)--(第三节)Spark几种运行模式

    spark应用执行机制分析 前段时间一直在编写指标代码,一直采用的是--deploy-mode client方式开发测试,因此执行没遇到什么问题,但是放到生产上采用--master yarn-clus ...

  9. shell脚本批量启动zookeeper

    脚本名称为zk_run.sh 将下面代码粘贴进zk_run.sh 添加执行权限 chmode +x zk_run.sh 运行脚本 ./zk_run.sh start 如果发现zookeeper没有启动 ...

  10. $python日期和时间的处理

    总结一下python中对日期和时间的常用处理方法. 准备 import time,datetime 常用操作 输出当前的日期时间 方式一: now = time.localtime() print ' ...