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 ...
随机推荐
- Xtts v4 xttdriver.pl & xtt.properties
Perl脚本xttdriver.pl选项的描述 下表描述了主要支持脚本xttdriver.pl的可用选项. 级备份.这些备份将写入xtt.properties变量"backupformat ...
- zabbix 千台服务器自动添加实战
一,模式 zabbix 的自动添加 主机有梁祝方式: 自动发现-----被动模式 由服务端主动发起,Zabbix Server开启发现进程,定时扫描局域网中IP服务器.设备, 自动注册----主动 ...
- (转)2017年最新企业面试题之shell(一,二)
2017年最新企业面试题之shell(一) ********************************************** 企业Shell面试题1:批量生成随机字符文件名案例 * *** ...
- core核心模块
5. core核心模块 核心模块会通过compiler模块提供的调用compiler的功能, 将用户的输入转为VM直接的输入 编译模块用来编译, 而核心模块用来执行 在core.h文件中 // 不需要 ...
- Aaja.pro 未定义
问题描述:安装新系统后,将代码迁至新系统,所有用到ajaxpro框架调用ajax方法时均报“xx未定义”的错: 解决问题的过程 : 1.看看你在前台调用的方法的命名空间,方法名和后台的是否对应.在后台 ...
- Hashtable元素的删除
2中方法 Remove(); Clear(); static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1,& ...
- centos解决The path "" is not a valid path to the 3.2.0-4-amd64 kernel headers.问题
我的机子炸了,然后我就得重新装我的虚拟机,再然后我就想去弄好我的共享文件夹安装vmtools,安装的时候出现了一个问题,我忘记以前是怎么解决的,又困扰了我好久 Searching for a vali ...
- 对HTML的大致了解
HTML的全称是Hyper Text Markup Language(超文本标记语言),是一种标记语言.其中,HTML文档一个重要的.广泛使用的标准HTML4.01是在1999年12月24日由W3C组 ...
- yeoman-bower-grunt之间的关系
npm install -g yo 前置技能 Node and NPM nodeJs就是基于谷歌v8引擎的一个javascript环境,使js不仅可以运行在浏览器端,也能在服务器端运行. NPM(No ...
- JsonConvert序列化问题
返回的Json数据如下: [[1400025600,9633460,9667535,2698.09,2734.73,2749,2698.08,25333.3057,11784.9,13548.4,69 ...