典型的两道矩阵快速幂求斐波那契数列

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【矩阵快速幂】的更多相关文章

  1. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  2. UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  3. 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 ...

  4. 用PL0语言求Fibonacci数列前m个中偶数位的数

    程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...

  5. C++项目參考解答:求Fibonacci数列

    [项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...

  6. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  7. 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: /  0                      n=0 f(n)=      ...

  8. 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 ...

  9. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

随机推荐

  1. Cakephp 创建无模型的Controller

    控制器(Controller)如果没有特定的表/模型关联的话,哪怕建测试都会出错,但你可以加一行到控制器(Controller)里就好了public $uses=array(); 或者 public ...

  2. IOS 获得通讯录中联系人的所有属性 备用参考

    ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...

  3. 计算S(n)=a+aa+aaa+...... 其中a是一个数字

    描述 计算S(n)=a+aa+aaa+...... 其中a是一个数字 输入数据 两个分别表示a和n的整数 输出数据 一个表示S(n)的整数 输入示例 3 5 输出示例 37035 # include ...

  4. [转载]初学C#之list

    C# List<T>用法 所属命名空间:System.Collections.Generic public class List<T> : IList<T>, IC ...

  5. CV牛人牛事简介之一

    CV牛人牛事简介之一 [论坛按] 发帖人转载自:http://doctorimage.cn/2013/01/01/cv-intro-niubility/#6481970-qzone-1-83120-8 ...

  6. location.search 详解

    JS中location.search什么意思 设置或获取网页地址跟在问号后面的部分 当以get方式在url中传递了请求参数时,可以利用location的search属性提取参数的值,下面的代码把参数的 ...

  7. PIXLCLOUND

    http://pixlcloud.com/main/career/ https://www.recordedfuture.com/siem-threat-intelligence-part-1/

  8. easyui源码翻译1.32--Droppable(放置)

    前言 使用$.fn.droppable.defaults重写默认值对象.下载该插件翻译源码 源码 /** * jQuery EasyUI 1.3.2 * *翻译:lbq --放置 拉伸 */ (fun ...

  9. gdb 调试c/c++的一些小技巧

    ptype obj/class/struct 查看obj/class/struct的成员,但是会把基类指针指向的派生类识别为基类   set print object on 这个选项可以看到派生对象的 ...

  10. 【HDOJ】1262 寻找素数对

    典型的二分决策树.而且本身两数和是偶数. #include <stdio.h> #include <string.h> #define MAXNUM 10001 int isP ...