UVaLive 7361 Immortal Porpoises (矩阵快速幂)
题意:求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 (矩阵快速幂)的更多相关文章
- UVaLive 7361(矩阵快速幂)
题意:矩阵快速幂求斐波那契数列. 分析:
- UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...
- UVALive 3704 细胞自动机 矩阵快速幂
是时候要做做数学类的题目了 这属于比较简单的矩阵快速幂了,因为有个已知的矩阵循环的结论,所以为了节约时空,只需要保留一行即可,这个稍微有点难写,也不是难写,主要是注意细节.其他的矩阵快速幂一下即可 # ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 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 ...
随机推荐
- RazorEngine 3.6.5.0
public class Person { public string Name { get; set; } public string Code { get; set; } } var templa ...
- WEB-INF目录与META-INF目录的作用
/WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...
- createElement 创建DOM元素
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- FTP出现211-Extension supported 停止的解决方法
FTP出问题211-Extension supported 停止的解决方法 FTP出问题211-Extension supported 停止的解决方法 FLASHFXP FTP上传登录时提示Exten ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- 03day2
03day1 不说了,图论题因为没有把加边的过程放到循环里导致只有 10 分.(不要吐槽我啊...) 竞赛排名 排序 [问题描述] [输入] 文件的第一行为参赛总人数 N(1≤N≤1000),从第 ...
- 【Java】Java处理double相加的结果异常
方式一(四舍五入):保留两位小数 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, ...
- ECSHOP v2.5数据库字典
ECSHOP v2.5 数据库字典 ECSHOP R&D Team 2007年4月16日 商品相关表 商品分类表 category 此表用来维护商品分类信息 字段名 字段描述 字段类型 默认值 ...
- UTF-8编码规则
UTF-8是Unicode的一种实现方式,也就是它的字节结构有特殊要求,所以我们说一个汉字的范围是0X4E00到0x9FA5,是指unicode值,至于放在utf-8的编码里去就是由三个字节来组织,所 ...
- HDU 4422 The Little Girl who Picks Mushrooms
题意:一共有5座山,已知小女孩在n座山采了n篮蘑菇,如果n小于5则在其他的山里采了任意重量的蘑菇,给出每篮蘑菇的重量,她回去的时候会遇到仨女巫要她交出三篮蘑菇的重量和恰好为1024的倍数,否则就把她的 ...