CodeForces405B - Jzzhu and Sequences 矩阵快速幂
就是说:
f1 = x; f2 = y; 然后 f(i) = f(i-1) + f(i+1)
化简一下式子:
f(i+1) = f(i) - f(i-1)
用i 替换 i+1, 则
f(i) = f(i-1) - f(i-2) 这样就差不多可斐波那契的矩阵快速幂了 构造一个矩阵
就是很裸的矩阵快速幂了
注意取膜啊阿啊阿啊阿啊
#include <stdio.h>
typedef long long LL;
long long read(){long long x = 0, f = 1;char ch=getchar();while(ch < '0' || ch > '9'){if(ch=='-') f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}return x * f;
}
const int maxn = 2;
const int mod = 1e9 + 7;
struct Matrix{long long int m[maxn][maxn];
}unit;
Matrix operator * (Matrix a, Matrix b){Matrix ret;LL x;int n = 2;for(int i = 0; i < n; ++i){for(int j = 0; j < n; ++j){x = 0;for(int k = 0; k < n; ++k){x += (((LL)a.m[i][k] * b.m[k][j]) + mod ) % mod;}ret.m[i][j] = (x + mod) % mod;}}return ret;
}
void init_unit(){// 单位矩阵for(int i = 0; i < maxn; ++i){unit.m[i][i] = 1;}
}
Matrix pow_mat(Matrix a, LL n){Matrix ans = unit;while (n) {if(n & 1){ans = ans * a;}a = a * a;n >>= 1;}return ans;
}
int main(){init_unit();Matrix ans, a;ans.m[0][0] = read();ans.m[0][1] = read();a.m[0][0] = 0; a.m[0][1] = -1;a.m[1][0] = 1; a.m[1][1] = 1;long long int n = read();a = pow_mat(a, n - 1);ans = ans * a;printf("%lld\n", (ans.m[0][0] + mod )% mod);return 0;
}
CodeForces405B - Jzzhu and Sequences 矩阵快速幂相关推荐
- H - Log Concave Sequences —— 矩阵快速幂
This way 题意: 你现在有无穷个0,1,2,现在让你构造一个长度为n的串,使得满足以下条件 对于第i位(2<=i<n),a[i]*a[i]>=a[i-1]*a[i+1] 问你 ...
- Log Concave Sequences(矩阵快速幂求递推)
Log Concave Sequences Gym - 102302H A sequence of numbers A is said to be logarithm concave if, and ...
- 矩阵快速幂2Jzzhu and Sequences Recurrences Contemplation! Algebra Reading comprehension
来!接着写题解,立个flag:矩阵快速幂题解不写完今晚不睡觉! 此题解是接着矩阵快速幂1写的,此篇有哪里写不清楚的地方导致看不懂的可以先看1:https://blog.csdn.net/qq_4536 ...
- (矩阵快速幂)CF102302H. Log Concave Sequences
CF102302H. Log Concave Sequences 题意&思路: 有n位数,每位只包含0,1,2,要求对于每个位置,ai2>=ai-1*ai+1.求这样的数有多少. 对于答 ...
- gym 102302 2019 USP-ICMC H-Log Concave Sequences (dp + 矩阵快速幂优化)
题目: 传送门 思路: 我们可以先写出转移方程,发现该方程是一个不变的递推式,我们考虑用矩阵快速幂来优化这个递推式. 完结撒花- AC_Code #include <iostream ...
- 矩阵快速幂各类题型总结(一般,共轭,1 * n, 矩阵简化)
Reading comprehension 这个不难找出递推式f[n]=f[n−1]+2f[n−2]+1f[n] = f[n - 1] + 2f[n - 2] + 1f[n]=f[n−1]+2f[n− ...
- poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ --by fraud DNA Sequence Time Limit: 1000MS Memory ...
- UVA10689 Yet another Number Sequence【数列+矩阵快速幂】
Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...
- 数论-快速幂、矩阵快速幂、慢速乘
文章目录 快速幂 矩阵快速幂 慢速乘 例题 HDU-2817 HDU-3117 XUJC-1395 快速幂 首先幂运算 a n a^n an就是 n n n个 a a a相乘,我们可以直接调用库函数 ...
- 2020.7.20【算协集训】矩阵快速幂
算法分析与排序 A - Fibonacci (POJ - 3070) 分析 代码 B - Tr A (HDU - 1575) 分析 代码 C - A Simple Math Problem (HDU ...
最新文章
- 不使用任何框架实现CNN网络
- 汇编语言随笔(13)- 外中断(可屏蔽中断)、实验15
- 【任务脚本】更新jd年兽任务脚本,京东淘宝活动任务全自动程序
- SpringBoot(七)整合themeleaf+bootstrap
- 前端和后端哪个工资高_前端、后端、全栈都是干嘛的?哪个薪资高?
- javascript里用php,php中使用javascript - 程序园
- 贝叶斯公式设b_数据分析经典模型——朴素贝叶斯
- lamp中php怎么连接mysql_LAMP分离安装环境下,php无法连接mysql的问题
- java框架----mybatis的使用(一)
- 第5讲:软考中高项04_进度管理、成本管理
- matlab求含参数一元三次方程,matlab 求解一元三次方程,带其他参数
- windows server 一键启用图片查看器
- ts定义数组类型_ts基本类型
- 华为模拟器eNSP练习题 - Hybrid
- Auto.js 实现钉钉实时抢红包
- 电商后台系统产品逻辑全解析
- oracle 数据库中 connect by 和level 的用法
- R语言时间序列基础库zoo
- spider test
- fedora RPM包下载地址