hdu 5950 Recursive sequence 递推式 矩阵快速幂
题目链接
题意
给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为
\]
思路
参考
将递推式改写$$\begin{pmatrix}f(n)\f(n-1)\n4\n3\n2\n\1\end{pmatrix}=\begin{pmatrix}1&2&1&4&6&4&1\1&0&0&0&0&0&0\0&0&1&4&6&4&1\0&0&0&1&3&3&1\0&0&0&0&1&2&1\0&0&0&0&0&1&1\0&0&0&0&0&0&1\end{pmatrix}\begin{pmatrix}f(n-1)\f(n-2)\(n-1)4\(n-1)3\(n-1)2\(n-1)\1\end{pmatrix}$$
然后上矩阵快速幂。
// 一开始令\(d_i=c_i-2c_{i-1}\),推出了一个\(d_i=(-1)^{i-2}(d_2-15)+\frac{1}{2}i^4+i^3-\frac{1}{2}i\),虽然接下来也好做但挺烦且容易算错。一上来就应该往矩阵快速幂的方向去想的。
Code
#include <bits/stdc++.h>
#define N 7
using namespace std;
typedef long long LL;
const LL mod = 2147493647;
typedef struct {
LL mat[N][N];
void init(LL x){
memset(mat, 0, sizeof(mat));
for(int i=0; i<N; i++) mat[i][i] = x;
}
void print() const {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) printf("%lld ", mat[i][j]); printf("\n");
}
}
} Matrix;
Matrix p = {1,2,1,4,6,4,1,
1,0,0,0,0,0,0,
0,0,1,4,6,4,1,
0,0,0,1,3,3,1,
0,0,0,0,1,2,1,
0,0,0,0,0,1,1,
0,0,0,0,0,0,1
};
LL add(LL a, LL b) { return (a + b + mod) % mod; }
LL mul(LL a, LL b) { return a * b % mod; }
Matrix mulm(const Matrix& a, const Matrix& b) {
Matrix temp;
temp.init(0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) temp.mat[i][j] = add(temp.mat[i][j], mul(a.mat[i][k], b.mat[k][j]));
}
}
return temp;
}
Matrix poww(LL n) {
Matrix a = p, ret;
ret.init(1);
while (n) {
if (n & 1) ret = mulm(ret, a);
a = mulm(a, a);
n >>= 1;
}
return ret;
}
void work() {
LL n, a, b;
scanf("%lld%lld%lld", &n, &a, &b);
Matrix m = poww(n-2);
LL ans = add(add(add(add(add(add(mul(b, m.mat[0][0]), mul(a, m.mat[0][1])), mul(16, m.mat[0][2])),
mul(8, m.mat[0][3])), mul(4, m.mat[0][4])), mul(2, m.mat[0][5])), m.mat[0][6]);
printf("%lld\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
hdu 5950 Recursive sequence 递推式 矩阵快速幂的更多相关文章
- HDU 5950 Recursive sequence 递推转矩阵
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5950 Recursive sequence 非线性递推式 矩阵快速幂
题目传送门 题目描述:给出一个数列的第一项和第二项,计算第n项. 递推式是 f(n)=f(n-1)+2*f(n-2)+n^4. 由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化
这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...
- HDU-6185-Covering(推递推式+矩阵快速幂)
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 6185 递推+【矩阵快速幂】
<题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
- [HDOJ2604]Queuing(递推,矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 递推式是百度的,主要是练习一下如何使用矩阵快速幂优化. 递推式:f(n)=f(n-1)+f(n- ...
- [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)
题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...
- hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)
[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...
随机推荐
- ES6箭头函数基本用法
ES6箭头函数基本用法 ``` window.onload = function(){ alert(abc); } //箭头函数 window.onload = ()=>{ alert(&quo ...
- Python 枚举类源码解析
1. EnumMeta 元类编程,生成类的类,可以动态生成类. 用法: type(name, bases, dict) name -> 类名: str bases -> 基类: tuple ...
- OpenCV学习笔记(七) 图像金字塔 阈值 边界
转自: OpenCV 教程 使用 图像金字塔 进行缩放 图像金字塔是视觉运用中广泛采用的一项技术.一个图像金字塔是一系列图像的集合 - 所有图像来源于同一张原始图像 - 通过梯次向下采样获得,直到达到 ...
- TCP/IP网络编程之多进程服务端(一)
进程概念及应用 我们知道,监听套接字会有一个等待队列,里面存放着不同客户端的连接请求,如果有一百个客户端,每个客户端的请求处理是0.5s,第一个客户端当然不会不满,但第一百个客户端就会有相当大的意见了 ...
- itchat 总结(转)
python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...
- 数据预处理之独热编码(One-Hot Encoding)
问题的由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑以下三个特征: ["male","female"] ["from ...
- 【Best Time to Buy and Sell Stock II】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Selenium 中 高亮元素
//高亮元素 WebElement element = driver.findElement(By.cssSelector(".table1 .btn-public label" ...
- Java 第七次
- 二维数组的动态分配(new)、初始化(memset)和撤销(delete)
来自http://blog.csdn.net/maverick1990/article/details/22829135 一维数组 动态分配,int *array = new int[10] 初始化, ...