http://wikioi.com/problem/1250/

我就不说这题有多水了。

0 1

1 1

矩阵快速幂

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } typedef int mtx[2][2];
void mul(mtx a, mtx b, mtx c, int la, int lb, int lc, int md) {
mtx t;
rep(i, la) rep(j, lc) {
t[i][j]=0;
rep(k, lb) t[i][j]=(t[i][j]+a[i][k]*b[k][j])%md;
}
rep(i, la) rep(j, lc) c[i][j]=t[i][j];
}
mtx a, b, c;
int main() {
int cs, n, q;
read(cs);
while(cs--) {
read(n); read(q);
a[0][0]=b[0][1]=b[1][0]=0;
a[0][1]=a[1][0]=a[1][1]=1;
b[0][0]=b[1][1]=1;
c[0][0]=0; c[0][1]=1;
while(n) {
if(n&1) mul(a, b, b, 2, 2, 2, q);
mul(a, a, a, 2, 2, 2, q);
n>>=1;
}
mul(c, b, c, 1, 2, 2, q);
printf("%d\n", c[0][1]);
}
return 0;
}

题目描述 Description

定义:f0=f1=1, fn=fn-1+fn-2(n>=2)。{fi}称为Fibonacci数列。

输入n,求fn mod q。其中1<=q<=30000。

输入描述 Input Description

第一行一个数T(1<=T<=10000)。

以下T行,每行两个数,n,q(n<=109, 1<=q<=30000)

输出描述 Output Description

文件包含T行,每行对应一个答案。

样例输入 Sample Input

3

6 2

7 3

7 11

样例输出 Sample Output

1

0

10

数据范围及提示 Data Size & Hint

1<=T<=10000

n<=109, 1<=q<=30000

【wikioi】1250 Fibonacci数列(矩阵乘法)的更多相关文章

  1. 1250 Fibonacci数列(矩阵乘法)

    1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, fn=fn-1+fn ...

  2. CODEVS1533 Fibonacci数列 (矩阵乘法)

    嗯,,,矩阵乘法最基础的题了. Program CODEVS1250; ..,..] of longint; var T,n,mo:longint; a,b:arr; operator *(a,b:a ...

  3. 矩阵乘法快速幂 codevs 1250 Fibonacci数列

    codevs 1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 定义:f0=f1=1 ...

  4. 1250 Fibonacci数列(矩阵乘法快速幂)

    1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 定义:f0=f1=1, f ...

  5. 1250 Fibonacci数列

    1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 定义:f ...

  6. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  7. [codevs]1250斐波那契数列<矩阵乘法&快速幂>

    题目描述 Description 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30 ...

  8. Loj10222 佳佳的Fibonacci(矩阵乘法)

    题面 给定\(n,m\),求: \[ T(n)=\sum_{i=1}^ni\times f_i \] 其中\(f_i\)为斐波那契数列的第\(i\)项 题解 不妨设: \[ S(n)=\sum_{i= ...

  9. P1349 广义斐波那契数列(矩阵乘法)

    题目 P1349 广义斐波那契数列 解析 把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了 \[\begin{bmatrix}f_2\\f_1 \end{bmatrix}\begin{bmatr ...

随机推荐

  1. django revision

    由于多次涉及到了这个东东,又不是很理解机制,决定深入研究下. what django-revision到底啥玩意?readthedocs上只有一句话概括:django-reversion can be ...

  2. string s = null 和 string s = “”的区别

    string s = null; 表示一个空串,没有占用了空间,不在内存中开辟空间 string s = "";在内存中开辟空间,但空间中没有值(""也是一个字 ...

  3. schedule CCCallfunc CCCallfuncN CCCallfuncND

    schedule(schedule_selector(HelloWorld::step), 1.0f); void HelloWorld::step(float dt) { CCLog("d ...

  4. block引发的陷阱

    block在项目的开发中使用时非常频繁的,苹果官方也极力推荐使用block.其实,究其本质,block就是指向结构体的指针(可利用运行时机制查看底层生成的c代码).然而在使用block时会存在很多陷阱 ...

  5. javascript ASCII和Hex互转

    <script> var symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@"; var loAZ ...

  6. CSRF和XSS

    XSS(跨站脚本攻击): 攻击者发现XSS漏洞——构造代码——发送给受害人——受害人打开——攻击者获取受害人的cookie——完成攻击 XSS是什么?它的全名是:Cross-site scriptin ...

  7. opencv学习笔记(五)镜像对称

    opencv学习笔记(五)镜像对称 设图像的宽度为width,长度为height.(x,y)为变换后的坐标,(x0,y0)为原图像的坐标. 水平镜像变换: 代码实现: #include <ios ...

  8. Android之Bundle类

    API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...

  9. undefined reference to 'typeinfo for android::Thread'

    原因:工程使用了系统库libstagefright.so,而该库是Android系统用no-rtti方式编译出来的,因此我们的工程也必须要用no-rtti方式编译. 解决方法:在Application ...

  10. ytu 1789:n皇后问题(水题,枚举)

    n皇后问题 Time Limit: 1 Sec  Memory Limit: 64 MB  Special JudgeSubmit: 12  Solved: 3[Submit][Status][Web ...