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的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  2. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  3. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  5. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  6. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  7. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  8. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

  9. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

随机推荐

  1. [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks

    感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...

  2. Linux系统守护进程详解ntsysv 可以关掉那些服务

    acpid, haldaemon, messagebus, klogd,network, syslogd  以上几个服务必须开启!其他的分析如下: 1.NetworkManager,NetworkMa ...

  3. Window.open 实现导航与打开窗口,导航到一个特定链接地址,也可以打开一个新的浏览器窗体

    语法 window.open(strUrl,strWindowName,strWindowFeatures ,replace) strUrl: 打开资源的地址 strWindowName: 表示窗体名 ...

  4. T4运行时模板

    可以通过Visual Studio运行时文本模板在您的应用程序在运行时生成文本字符串. 执行应用程序的计算机不必具有 Visual Studio. 运行库模板有时称为"预处理文本模板&quo ...

  5. 使用Unity在MVC上实现动态注入

    一.前言 通过前一篇的文章介绍使用unity轻量级的依赖注入容器,本文就介绍在MVC上使用unity依赖注入控制器和控制器中的日志属性. 实现MVC中新提供 的两个接口:IDependencyReso ...

  6. CSS 设计彻底研究(一)(X)HTML与CSS核心基础

    第1章 (X)HTML与CSS核心基础 这一章重点介绍了4个方面的问题.先介绍了 HTML和XHTML的发展历程以及需要注意的问题,然后介绍了如何将CSS引入HTML,接着讲解了CSS的各种选择器,及 ...

  7. Memcache入门知识

    Memcache适合做缓存,是一款管理内存的很小的软件,实现对内存数据的管理,一般我们用memcache存储临时数据,因为内存不能储存永久化的数据,内存里面的数据,断电就消失了. memcache可以 ...

  8. Lucene学习总结之六:Lucene打分公式的数学推导

    在进行Lucene的搜索过程解析之前,有必要单独的一张把Lucene score公式的推导,各部分的意义阐述一下.因为Lucene的搜索过程,很重要的一个步骤就是逐步的计算各部分的分数. Lucene ...

  9. 我的第一个comet长连接例子

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. WordPress插件制作笔记(三)---Stars Comments Article

    wp 文章星级评价 插件 下载地址4:http://pan.baidu.com/s/1eQnGIGU [articles_star_vote_score_optiontable_serialize_c ...