POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列
POJ 那是 默认a=0,b=1
UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a);
//POJ
#include <cstdio>
#include <iostream> using namespace std; const int MOD = ; struct matrix
{
int m[][];
}ans, base; matrix multi(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ; ++i)
{
for(int j = ; j < ; ++j)
{
tmp.m[i][j] = ;
for(int k = ; k < ; ++k)
tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;
}
}
return tmp;
}
int fast_mod(int n) // 求矩阵 base 的 n 次幂
{
base.m[][] = base.m[][] = base.m[][] = ;
base.m[][] = ;
ans.m[][] = ans.m[][] = ; // ans 初始化为单位矩阵
ans.m[][] = ans.m[][] = ;
while(n)
{
if(n & ) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t
{
ans = multi(ans, base);
}
base = multi(base, base);
n >>= ;
}
return ans.m[][];
} int main()
{
int n;
while(scanf("%d", &n) && n != -)
{
printf("%d\n", fast_mod(n));
}
return ;
}
//uva
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=1e5+;
const double eps=1e-;
const double pi=acos(-);
#define ll long long
const int MOD = ; int a,b,n,m;
struct matrix
{
int m[][];
} ans, base; matrix multi(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ; ++i)
{
for(int j = ; j < ; ++j)
{
tmp.m[i][j] = ;
for(int k = ; k < ; ++k)
tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;
}
}
return tmp;
}
int fast_mod(int n) // 求矩阵 base 的 n 次幂
{
base.m[][] = base.m[][] = base.m[][] = ;
base.m[][] = ;
ans.m[][] = ans.m[][] = ; // ans 初始化为单位矩阵
ans.m[][] = ans.m[][] = ;
while(n)
{
if(n & ) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t
{
ans = multi(ans, base);
}
base = multi(base, base);
n >>= ;
}
// return ans.m[0][1];
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&a,&b,&n,&m);
m = pow(, m);
if (n == )
{
printf("%d\n", (a%m + m) % m);
continue;
}
if (n == )
{
printf("%d\n", (b%m + m) % m);
continue;
}
fast_mod(n-);
int tmp = ((ans.m[][] * b + ans.m[][] * a) % m + m) % m;
printf("%d\n", tmp);
}
return ;
}
POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】的更多相关文章
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...
- 用PL0语言求Fibonacci数列前m个中偶数位的数
程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...
- C++项目參考解答:求Fibonacci数列
[项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]
作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: / 0 n=0 f(n)= ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
随机推荐
- 2014年度辛星css教程夏季版第五节
本小节我们讲解css中的”盒模型“,即”box model“,它通常用于在布局的时候使用,这个”盒模型“也有人成为”框模型“,其实原理都一样,它的大致原理是这样的,它把一个HTML元素分为了这么几个部 ...
- Java 代码优化过程的实例介绍
衡量程序的标准 衡量一个程序是否优质,可以从多个角度进行分析.其中,最常见的衡量标准是程序的时间复杂度.空间复杂度,以及代码的可读性.可扩展性.针对程序的时间复杂度和空间复杂度,想要优化程序代码,需要 ...
- c++ 内存泄露的检查
对于c++的内存泄露检测,除了我们自己手动检查以外,还可以使用c++中的函数来帮助我们检测, 如下代码: #include "stdafx.h" #include <stri ...
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Linux下eclipse的安装以及配置
在安装好jdk并配置好后,就可以进行eclipse的安装了,其步骤如下: 1.下载eclipse 我所用的eclipse为:eclipse-dsl-juno-SR1-linux-gtk.tar 2. ...
- POJ3663
题意简单. 关键:记录每头牛的val值,每次寻找和某头牛匹配的牛时候,可以通过刚刚记录的值来计算. #include<stdio.h> #include<string.h> # ...
- Mysql Not in有null值查询的问题
今天发现Mysql的not in使用的一个问题,大致是: select * from A where id not in (select fid from B). 发现查询结果无论如何都是0条记录.后 ...
- eCos中断模型
http://blog.csdn.net/chychc/article/details/8313458 http://www.cnblogs.com/RandyQ/archive/2013/04/14 ...
- laravel route路由,视图和response和filter
Laravel充分利用PHP 5.3的特性,使路由变得简单并富于表达性.这使得从构建API到完整的web应用都变得尽可能容易.路由的实现代码在 application/routes.php 文件. 和 ...
- NuGet学习笔记(1)——初识NuGet及快速安装使用
关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...