【wikioi】1250 Fibonacci数列(矩阵乘法)
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数列(矩阵乘法)的更多相关文章
- 1250 Fibonacci数列(矩阵乘法)
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, fn=fn-1+fn ...
- CODEVS1533 Fibonacci数列 (矩阵乘法)
嗯,,,矩阵乘法最基础的题了. Program CODEVS1250; ..,..] of longint; var T,n,mo:longint; a,b:arr; operator *(a,b:a ...
- 矩阵乘法快速幂 codevs 1250 Fibonacci数列
codevs 1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1 ...
- 1250 Fibonacci数列(矩阵乘法快速幂)
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, f ...
- 1250 Fibonacci数列
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 定义:f ...
- 斐波那契数列 矩阵乘法优化DP
斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007\),\(n\le 10^{18}\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...
- [codevs]1250斐波那契数列<矩阵乘法&快速幂>
题目描述 Description 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30 ...
- Loj10222 佳佳的Fibonacci(矩阵乘法)
题面 给定\(n,m\),求: \[ T(n)=\sum_{i=1}^ni\times f_i \] 其中\(f_i\)为斐波那契数列的第\(i\)项 题解 不妨设: \[ S(n)=\sum_{i= ...
- P1349 广义斐波那契数列(矩阵乘法)
题目 P1349 广义斐波那契数列 解析 把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了 \[\begin{bmatrix}f_2\\f_1 \end{bmatrix}\begin{bmatr ...
随机推荐
- linux下统计当前目录下文件个数
ls | wc -l 转自:http://bbs.csdn.net/topics/60387132
- HAST 使用笔记
1.环境 2台 freebsd 9.2的机器,/home分区为260G,需要将其转为hast块设备 2.安装 (1)先umount /home,然后注释掉/etc/fstab上的/home记录: # ...
- HTTP状态码整理
状态消息 1xx:信息 消息 描述 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. 101 Switching Protocols 服 ...
- Hadoop学习:
文件名称 格式 描述 hadoop-env.sh BaSh 脚本 记 录 脚 本 要 用 的 环 境 变 , 以 运 行Hadoop core-site.xml HadooP配XML Hadoop C ...
- ligureUI 刷新列求和
dataGrid=$("#dataGrid").ligerGrid({ columns: [ {display:, align:'left' }, {display:, align ...
- 在竞赛ACM Java处理输入输出
一.Java之ACM注意点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非 ...
- 编译预处理命令--define和ifdef的使用
这里将对常用的预处理命令进行学习. 一.宏定义 ·defined 格式:`defined 宏名 数值 或者 `define 宏名 注意:后面没有‘;‘,和单片机不一样: ...
- TinyHttpd中sockaddr与struct sockaddr_in的区别
上午学习TinyHttpd的源码,sockaddr 结构体以前没接触过, 在网络编程中经常用到struct sockaddr和struct sockaddr_in,这里简单介绍. 在linux环境下, ...
- oracle 10g 学习之游标使用和异常介绍(11)
一.游标 1. 使用游标 要求: 打印出 80 部门的所有的员工的工资: salary: xxx declare --1. 定义游标 cursor salary_cursor is select sa ...
- UML从需求到实现----用例
关于用例图的概念相信不用我去说了 .能看到这篇文章的都是知道用例图概念的人. UML 中最重要的是什么图呢 ?毫无疑问应该是用例图 ,用例是后期时序图 和实际开发的重要依据. 说明一下用例图是怎么产生 ...