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. XCode Could not launch &quot;&quot; failed to get the task for process

    在Xcode下编译project正常,在模拟器下执行正常,最后在真机上执行的时候出现了例如以下错误: Could not launch "FeedMeWorms" failed t ...

  2. BOOST 线程完全攻略 - 结束语

    modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...

  3. windows下用vs2008和boost结合编译程序

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://co63oc.blog.51cto.com/904636/504469 win ...

  4. ora-24247:网络访问被访问控制列表(ACL)拒绝

    用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...

  5. .net 地址栏传中文乱码 的解决方法

    1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" res ...

  6. Android SQLite的使用2(非原创)

    1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...

  7. 返回某个界面——nav

     NSInteger index=[[self.navigationController viewControllers]indexOfObject:self];  [self.navigationC ...

  8. Unity-碰撞

    固定位置和角度 选择项目视图中的 Prefab. 打开检视面板中的 Rigidbody 标签可以看到 Constraints 项. 点击左边的三角形图标, 下面会进一步显示 Freeze Positi ...

  9. SQL实现递归及存储过程中In()参数传递解决方案[转]

    SQL实现递归及存储过程中In()参数传递解决方案   1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理.   -->实现: 假设 ...

  10. JuPyter(IPython) Notebook中通过pip安装第三方Python Module

    JuPyter(IPython) Notebooks中使用pip安装Python的模块 刚开始接触JuPyter Notebook的时候觉得这是个不错的写技术博客的工具,可以很直观的把代码和结果结合在 ...