POJ 3070 矩阵快速幂
题意:求菲波那切数列的第n项。
分析:矩阵快速幂。

右边的矩阵为a0 ,a1,,,
然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列。
#include <cstdio>
#include <cstring> using namespace std; typedef long long ll; const int maxn = ;
const int maxm = ; struct Matrix {
int n,m;
int a[maxn][maxm]; void clear() {
n = m = ;
memset(a,,sizeof(a));
} Matrix operator + (const Matrix &b) const {
Matrix tmp;
tmp.n = n;
tmp.m = m; for(int i=;i<n;++i)
for(int j=;j<m;++j)
tmp.a[i][j] = a[i][j] + b.a[i][j]; return tmp;
} Matrix operator - (const Matrix &b) const {
Matrix tmp;
tmp.n = n;
tmp.m = m; for(int i=;i<n;++i)
for(int j=;j<m;++j)
tmp.a[i][j] = a[i][j] - b.a[i][j]; return tmp;
} Matrix operator * (const Matrix & b) const {
Matrix tmp;
tmp.clear();
tmp.n = n;
tmp.m = b.m; for(int i=;i<n;++i)
for(int j=;j<b.m;++j)
for(int k=;k<m;++k)
tmp.a[i][j] +=(a[i][k]*b.a[k][j])%; return tmp;
} Matrix operator ^ (const int& k) const {
Matrix tmp,t = *this; int p = k;
tmp.clear(); tmp.m = tmp.n = this->n; for(int i=;i<n;++i)
tmp.a[i][i] = ; while(p) {
if(p&) tmp = tmp*t;
p>>=;
t = t*t;
}
return tmp;
}
}; int main(int argc, char const *argv[])
{
int n;
while(true) {
scanf("%d",&n);
if(n==-) break;
Matrix f;
f.clear();
f.n = f.m = ;
f.a[][] = ;
f.a[][] = ;
f.a[][] = ;
f.a[][] = ; f = f^n;
Matrix x;
x.clear();
x.n = ;
x.m = ;
x.a[][] = ;
x.a[][] = ;
f = f*x; printf("%d\n", f.a[][]); }
return ;
}
POJ 3070 矩阵快速幂的更多相关文章
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- 解题报告:poj 3070 - 矩阵快速幂简单应用
2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...
- poj 3070 矩阵快速幂模板
题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- poj 3734 矩阵快速幂+YY
题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...
- POJ 3233 矩阵快速幂&二分
题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...
- poj 3744 矩阵快速幂+概率dp
题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...
- Blocks(POJ 3734 矩阵快速幂)
Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
题目链接 请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...
随机推荐
- Mastering the Game of Go 论文阅读笔记
主要思想:用状态评估减少搜索深度,用动作采样减少搜索宽度. 参考文献:https://blog.csdn.net/songrotek/article/details/51065143
- vue中watch的使用
vue中watch的使用 vue中的watch是一个比较重要的概念,通过他我们可以检测data的变化,下面进行详细的介绍. watch定义方式如下: {[key: string]: string | ...
- [TimesTen]TT7001: User authentication failed
在使用sqldeveloper连接TimesTen一直报[TimesTen][TimesTen 11.2.2.8.0 ODBC Driver][TimesTen]TT7001: User authen ...
- Java Swing笔记
想到了解一下GUI主要是想用来做点小工具,记录一些笔记. 文本框自动换行和滚动条 private static JTextArea addJTextArea(JPanel panel, int x, ...
- Unity游戏项目常见性能问题
Unity技术支持团队经常会对有需求的客户公司项目进行游戏项目性能审查与优化,在我们碰到过的各种项目相关的问题中也有很多比较共同的方面,这里我们罗列了一些常见的问题并进行了归类,开发者朋友们可以参考下 ...
- elasticSearch请求流程图
- Redis启动和关闭
带配置文件启动 ./redis-server redis.conf 关闭 无密码模式 ./redis-cli -h xxx -p xxx shutdown 密码模式 ./redis-cli -h ...
- this 显示绑定
function foo (el) { console.log(el, this.id); } var obj = { id: 'aaa' }; [2,6,3].forEach(foo, obj); ...
- 深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
本篇是ECMA-262-3 in detail系列的一个概述(本人后续会翻译整理这些文章到本系列(第11-19章).每个章节都有一个更详细的内容链接,你可以继续读一下每个章节对应的详细内容链接进行更深 ...
- Python爬虫之三种数据解析方式
一.引入 二.回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需 ...