矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070
按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std; const int maxn = 110; struct matrix
{
int mat[3][3];
void init()
{
memset(mat,0,sizeof(mat));
mat[1][1] = mat[2][2] = 1;
}
}a; matrix mul(matrix a, matrix b)
{
matrix res;
memset(res.mat,0,sizeof(res.mat));
for(int i = 1; i <= 2; i++)
{
for(int k = 1; k <= 2; k++)
{
if(a.mat[i][k] == 0) continue;
for(int j = 1; j <= 2; j++)
{
int t = a.mat[i][k]*b.mat[k][j];
res.mat[i][j] = (res.mat[i][j] + t)%10000;
}
}
}
return res;
} matrix pow(matrix a, int n)
{
matrix res;
res.init(); while(n)
{
if(n&1)
res = mul(res,a);
a = mul(a,a);
n >>= 1;
}
return res;
} int main()
{
int n;
while(~scanf("%d",&n))
{
if(n == -1) break;
a.mat[1][1] = a.mat[1][2] = a.mat[2][1] = 1;
a.mat[2][2] = 0; matrix ans = pow(a,n); printf("%d\n",ans.mat[1][2]);
}
return 0;
}
矩阵经典题目六:poj 3070 Fibonacci的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵相乘
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7715 Accepted: 5474 Descrip ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
随机推荐
- 禁用UITableViewCell 重用机制
有时候不想让Cell重用,怎么办勒.接下来介绍两种方法 方法一 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt ...
- 【剑指offer】替换字符串中的空格
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25002199 剑指offer上的第四道题目,在九度OJ上測试通过,但还是有些问题.由于是用 ...
- [WebGL入门]十九,遮挡剔除和深度測试
注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外.鄙人webgl研究还不够深入,一些专业词语,假设翻译有误.欢迎大家指 ...
- [SASS] Make a responsive arrow box
Check the page:http://www.cssarrowplease.com/ In HTML: {{type}} is tow way binding in Angular, three ...
- jquery mouseout和mouseleave区别
mouseout()和mouseleave() 都是鼠标移出元素时触发,但是这两者又有所区别,需要大家留意: 不论鼠标指针离开指定元素还是该元素子元素,都会触发 mouseout 事件. 只有在鼠标指 ...
- jquery.validate的使用
在页面上面引用 <script type="text/JavaScript" src="js/jQuery.js"></script> ...
- Global.asax使用2
ASP.NET中利用Application和Session统计在线人数.历史访问量 先来简单说一下ASP.NET中的Application和Session 下图是我们非常熟悉的Web应用程序的结构: ...
- PHP学习笔记二十九【接口】
<?php //定义接口 //接口可以定义属性,但必须是常量而且是public //接口的所有方法必须是public interface Iusb{ public function start( ...
- Webfrom基础知识
MyBeNASP.NET内置对象 (1)简述ASP.NET内置对象. 答:ASP.NET提供了内置对象有Page.Request.Response.Application.Session.Server ...
- OpenCV——IplImage
IplImage结构: typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; /* 版本 (=0)*/ int nChannel ...