题意:求Fibonacci的第 n 项。

析:矩阵快速幂,如果不懂请看http://www.cnblogs.com/dwtfukgv/articles/5595078.html

是不是很好懂呢。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Matrx{
LL x[3][3];
void init(){
memset(x, 0, sizeof(x));
}
}; Matrx mul(const Matrx &lhs, const Matrx &rhs){
Matrx c;
c.init();
for(int i = 0; i < 2; ++i){
for(int j = 0; j < 2; ++j){
for(int k = 0; k < 2; ++k){
c.x[i][j] += lhs.x[i][k] * rhs.x[k][j];
c.x[i][j] %= mod;
}
}
}
return c;
} Matrx fast_pow(Matrx x, LL b){
Matrx ans, k = x;
ans.x[0][0] = ans.x[10][0] = ans.x[0][1] = 1;
ans.x[1][1] = 0; while(b){
if(b & 1){
ans = mul(ans, k);
}
b >>= 1;
k = mul(k, k);
} return ans;
} int main(){
Matrx mx;
mx.x[0][0] = mx.x[0][1] = mx.x[1][0] = 1;
mx.x[1][1] = 0;
Matrx nx;
nx.init();
nx.x[0][0] = 1; nx.x[0][1] = 0;
int T; cin >> T;
while(T--){
LL n;
scanf("%d %lld", &m, &n);
printf("%d ", m);
if(1LL == n){ printf("1\n"); continue; }
else if(2LL == n){ printf("1\n"); continue; }
Matrx ans = fast_pow(mx, n-2);
ans = mul(ans, nx);
printf("%lld\n", ans.x[0][0]);
}
return 0;
}

  

这也是运用矩阵快速幂的思想写的。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 1e9;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<LL, LL> ans; LL dfs(LL n){
if(ans.count(n)) return ans[n];
LL k = n/2LL;
if(n % 2 == 0) return ans[n] = (dfs(k) * dfs(k) + dfs(k-1)* dfs(k-1)) % mod;
else return ans[n] = (dfs(k) * dfs(k+1) + dfs(k-1) * dfs(k)) % mod;
} int main(){
ans[0] = ans[1] = 1;
int T; cin >> T;
while(T--){
LL x;
scanf("%d %lld", &m, &x);
printf("%d %lld\n", m, dfs(x-1));
}
return 0;
}

  

UVaLive 7361 Immortal Porpoises (矩阵快速幂)的更多相关文章

  1. UVaLive 7361(矩阵快速幂)

    题意:矩阵快速幂求斐波那契数列. 分析:

  2. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  3. UVALive 3704 细胞自动机 矩阵快速幂

    是时候要做做数学类的题目了 这属于比较简单的矩阵快速幂了,因为有个已知的矩阵循环的结论,所以为了节约时空,只需要保留一行即可,这个稍微有点难写,也不是难写,主要是注意细节.其他的矩阵快速幂一下即可 # ...

  4. LA 3704细胞自动机——循环矩阵&&矩阵快速幂

    题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...

  5. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  6. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  7. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  8. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  9. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

随机推荐

  1. Java 并发基础

    Java 并发基础 标签 : Java基础 线程简述 线程是进程的执行部分,用来完成一定的任务; 线程拥有自己的堆栈,程序计数器和自己的局部变量,但不拥有系统资源, 他与其他线程共享父进程的共享资源及 ...

  2. ASP.NET MVC 学习5、登陆页面改为SSO验证

    单点登录(SSO,single sign-on)是一个会话或用户身份验证过程,用户只需要登录一次就可以访问所有相互信任的应用系统,二次登录时无需重新输入用户名和密码.简化账号登录过程并保护账号和密码安 ...

  3. MSSQL大全

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  4. [swustoj 1023] Escape

    Escape     Description BH is in a maze,the maze is a matrix,he wants to escape! Input The input cons ...

  5. main cannot be resolved or is not a field

    今天在做XML解析的时候,总是给我报 XML Parsing Error: XML or text declaration not at start of entity 的错误,后来查了下讲大概意思是 ...

  6. 【有趣~】SFOJ-1711 Obey的恋爱、NYOJ-739 笨蛋难题

    笨蛋难题四 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...

  7. Devexpress GridControl中combobox级联显示 z

    http://minmin86121.blog.163.com/blog/static/4968115720143163533356/ 在 使用GridControl时,可能会有需求要求某2列显示co ...

  8. [C++]cin读取回车键

    最近碰到一个问题,就是从控制台读取一组数,如: 12 23 34 56 若是使用 int data; while ( cin >> data ) {//...} 当回车后,不能有效转换到后 ...

  9. UVA 11600-Masud Rana(状压,概率dp)

    题意: 有n个节点的图,开始有一些边存在,现在每天任意选择两点连一条边(可能已经连过),求使整个图联通的期望天数. 分析: 由于开始图可以看做几个连通分量,想到了以前做的一个题,一个点代表一个集合(这 ...

  10. 常用sql命令

    --1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话 CREATE TABLE student (     [id] [int] IDENTITY(1,1) NOT NU ...