解题报告:poj 3070 - 矩阵快速幂简单应用
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 - 矩阵快速幂简单应用的更多相关文章
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- POJ 3070 矩阵快速幂
题意:求菲波那切数列的第n项. 分析:矩阵快速幂. 右边的矩阵为a0 ,a1,,, 然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列. #include <c ...
- 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 ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- 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 ...
随机推荐
- Spark源码分析 – Checkpoint
CP的步骤 1. 首先如果RDD需要CP, 调用RDD.checkpoint()来mark 注释说了, 这个需要在Job被执行前被mark, 原因后面看, 并且最好选择persist这个RDD, 否则 ...
- python - 用户交互/数据类型/格式化输出/运算符/流程控制单双多分支
python:用户交互: 等用户输入,做反应: username=input("username:")password=input("password:")pr ...
- SpringBoot与消息(RabbitMQ)
1. JMS和AMQP JMS(Java Message Service): ActiveMQ是JMS实现; AMQP(Advanced Message Queuing Protocol) 兼容JMS ...
- django将数据库中数据直接传到html
1.当然,前提是建立和配置好django数据库啦~ 2.在python后台函数中引入需要的表 #要读取home这个APP下的models.py文件,引入其中的Student_message_unedi ...
- MFC Spin 控件
一般应用: 设置属性: Auto Buddy(自动取关联控件为TAB顺序前一个)Set Buddy Interger(使控件设置关联控件数值,这个值可以是十进制或十六进制)Wrap(数值超过范围时循环 ...
- Secure Sockets Layer(安全套接层)
SSL SSL(Secure Sockets Layer安全套接层)及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TL ...
- oracle入门(7)——存储过程
[本文介绍] 熟悉了PL/SQL语法后,实现java调用oracle存储过程才是主要目的.本文将介绍如何写存储过程,java如何调用存储过程. [存储过程介绍] 抛开专业的描述,存储过程就是在数据库里 ...
- IT开发工程师的悲哀现状和可能前途
IT开发工程师的悲哀现状和可能前途 本文所指的开发工程师,仅指程序开发人员和以数字电路开发为主的电子工程师.当你选择计算机或者电子.自控等专业进入大学时,你本来还是有机会从事其它行业的,可你毕业时执迷 ...
- java 多线程 day16 CountDownLatch 倒计时计数器
import java.util.concurrent.CountDownLatch;import java.util.concurrent.CyclicBarrier;import java.uti ...
- phpstudy2016 redis扩展 windows
第一步,查看环境的信息. 第二步,根据线程是否安全.架构32位或64位下载redis扩展. http://pecl.php.net/package-stats.php 第三步,php_redis.dl ...