poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用。
对于矩阵乘法与递推式之间的关系:
如:在斐波那契数列之中
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(矩阵快速幂)的更多相关文章
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂
正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- 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] ...
随机推荐
- tomcat用虚拟目录方式发布项目与manager页面配置
conf/Catalina/localhost:指定项目的配置信息 1.添加:ROOT.xml 听见Context节点: <Context docBase="/usr/local/to ...
- [BAT]远程执行或停止计划任务
执行 schtasks /run /tn "IPADForAdvisor_QA_APITest" /s SZPCWIN2K801 /u msdomain1\jzhang6 /p j ...
- 五个步骤搞定敏捷UX设计
互联网产品发展的速度越来越快,人们对于产品的要求也在不断的升级,这直接地导致了用户体验设计的重要性不断提升.与此同时,过去的流程冗长的设计开发模式已经不能够满足快速迭代的需要.<敏捷宣言> ...
- ipconfig 查看本机IP地址
打开cmd 窗口 然后输入ipconfig 就会为你展示你想要的IP地址了...
- mybatis 和 mybatis-spring
一. 1. 实体bean package com.mybatisBean; public class User { private Integer id; private String name; ...
- FIR IP
(1)多通道系数处理 系数的格式 (1) 每组系数的长度必须一样长: (2)多组系数依次连接即可; 添加多组系数后会,FIR IP核在生成时会多出以下几个信号 .s_axis_config ...
- nexus 私服跑一跑流程
尝试建立新项目上传,分享 D:\dev\workspace\helloworld>tree /F 卷 DATAPART 的文件夹 PATH 列表 卷序列号为 2C22-0A1A D:. │ po ...
- 4) Maven 安装
# ----------------------------------------------------------------------------# Maven2 Start Up Batc ...
- MapGIS Mobile开发
1. 先将Android开发环境配置好(包括Java + Eclipse + Android SDK) 2. 加载API类库(运行MapGIS 10 AndroidSDK.exe可以加载Mobile框 ...
- struts2和JSON的数据交互
一.实验环境 1.struts2基本包 2.json-plugin 在struts2的lib下可以找到. 3.web.xml 加入struts2 <filter> <filter-n ...