矩阵快速幂 POJ 3070 Fibonacci
题目传送门
1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了。效率很高啊 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 const int MOD = 10000; 13 struct Mat { 14 int m[2][2]; 15 }; 16 17 Mat multi_mod(Mat a, Mat b) { 18 Mat ret; memset (ret.m, 0, sizeof (ret.m)); 19 for (int i=0; i<2; ++i) { 20 for (int j=0; j<2; ++j) { 21 for (int k=0; k<2; ++k) { 22 ret.m[i][j] = (ret.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD; 23 } 24 } 25 } 26 return ret; 27 } 28 29 int matrix_pow_mod(Mat x, int n) { 30 Mat ret; ret.m[0][0] = ret.m[1][1] = 1; ret.m[0][1] = ret.m[1][0] = 0; 31 while (n) { 32 if (n & 1) ret = multi_mod (ret, x); 33 x = multi_mod (x, x); 34 n >>= 1; 35 } 36 return ret.m[0][1]; 37 } 38 39 int main(void) { //POJ 3070 Fibonacci 40 int n; 41 while (scanf ("%d", &n) == 1) { 42 if (n == -1) break; 43 Mat x; 44 x.m[0][0] = x.m[0][1] = x.m[1][0] = 1; x.m[1][1] = 0; 45 printf ("%d\n", matrix_pow_mod (x, n)); 46 } 47 48 return 0; 49 }
转载于:https://www.cnblogs.com/Running-Time/p/4693092.html
矩阵快速幂 POJ 3070 Fibonacci相关推荐
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- POJ 3070 Fibonacci(矩阵快速幂入门、模板)
? 题目链接:http://poj.org/problem?id=3070 ? 这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...
- I-Matrix Power Series POJ - 3233 矩阵快速幂+分治
I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...
- POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]
http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...
- poj3070 Fibonacci 矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18084 Accepted: 12572 Descr ...
- SDNU 1062.Fibonacci(矩阵快速幂)
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. I ...
- POJ3070 Fibonacci(矩阵快速幂)
用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...
- (矩阵快速幂)解所有类似Fibonacci 的题目
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ3070 Fibonacci【矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...
最新文章
- iOS架构-静态库.a之依赖第三方静态库.a的制作(8)
- swift_030(Swift 的访问控制)
- javaScript(1):基础部分
- hdu 1028 Ignatius and the Princess III 母函数入门
- 2016/1/4 学习笔记 数据类型转换 注释 语句
- (组合数求模=乘法逆元+快速幂) Problem Makes Problem
- Serverless 实战 —— 基于 Serverless + 企业微信打造 nCoV 疫情监控小助手
- java 子目录_Java:子目录点
- Airflow 中文文档:调度和触发器
- python中avg函数的使用_SQL AVG()
- java properties null_正确使用Java Properties - Java综合 - Java - JavaEye...
- Android 原生 MediaPlayer 和 MediaCodec 的区别和联系(二)
- 浅谈人工智能:现状、任务构与统一
- 隧道监控计算机网络系统方案,高速公路隧道监控系统解决方案
- 如何简单快速地调整图片大小
- 权力纷争必须上演公章大战?其实duck不必
- 谷歌ai人工智能叫什么_Google DeepMind在全球使用AI的10种方式
- 通过bitset库实现sha256
- ios设置音乐audio自动播放
- Ubuntu与arm开发板之间构建NFS数据通道
热门文章
- Apollo进阶课程㉚丨Apollo ROS背景介绍
- 华为虚拟服务器lanip地址,2018软考网络工程师《华为基础实验》十九配置路由器为DHCPServer...
- mysql源码包和二进制包_Linux软件包(源码包和二进制包)
- 信号模型噪声服从零均值高斯分布_非高斯噪声下基于分数低阶循环谱的调制识别方法...
- leetcode350. 两个数组的交集 II
- 《Python Cookbook 3rd》笔记(4.9):排列组合的迭代
- 《Python Cookbook 3rd》笔记(2.5): 字符串搜索和替换
- 发票管理软件_企业为什么需要ERP企业管理软件?
- C语言深度剖析书籍学习记录 第五章 内存管理
- Redis Cluster集群模式