CodeForces 392C Yet Another Number Sequence 矩阵快速幂
题意:
\(F_n\)为斐波那契数列,\(F_1=1,F_2=2\)。
给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\)。
求\(A_1+A_2+ \cdots + A_n\)。
分析:
构造一个列向量,
\({\begin{bmatrix}
F_{i-1}i^0 &
F_{i-1}i^1 &
\cdots &
F_{i-1}i^k &
F_{i}i^0 &
F_{i}i^1 &
\cdots &
F_{i}i^k &
S_{i-1}
\end{bmatrix}}^T\)
转移到列向量:
\({\begin{bmatrix}
F_{i}i^0 &
F_{i}i^1 &
\cdots &
F_{i}i^k &
F_{i+1}i^0 &
F_{i+1}i^1 &
\cdots &
F_{i+1}i^k &
S_{i}
\end{bmatrix}}^T\)
上半部分直接复制到上面去即可,考虑下半部分:
\(F_{i+1}(i+1)^k=(F_{i-1}+F_i)(i+1)^k=F_{i-1}\left [ (i-1)+2 \right ]^k + F_i(i+1)^k=F_{i-1} \sum C_k^j (i-1)^j 2^{k-j} + F_i \sum C_k^j i^j\)
最后\(S_i=S_{i-1}+F_i i^k\)
预处理一下组合数,按照上面的系数构造矩阵即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
const int maxsz = 90;
LL n;
int k, sz;
LL mul(LL a, LL b) { return a * b % MOD; }
LL add_mod(LL a, LL b) { a += b; if(a >= MOD) a -= MOD; return a; }
void add(LL& a, LL b) { a += b; if(a >= MOD) a -= MOD; }
struct Matrix
{
LL a[maxsz][maxsz];
Matrix() { memset(a, 0, sizeof(a)); }
Matrix operator * (const Matrix& t) const {
Matrix ans;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++)
for(int k = 0; k < sz; k++)
add(ans.a[i][j], mul(a[i][k], t.a[k][j]));
return ans;
}
void output() {
printf("sz = %d\n", sz);
for(int i = 0; i < sz; i++) {
for(int j = 0; j < sz - 1; j++)
printf("%d ", a[i][j]);
printf("%d\n", a[i][sz - 1]);
}
}
};
Matrix pow_mod(Matrix a, LL p) {
Matrix ans;
for(int i = 0; i < sz; i++) ans.a[i][i] = 1;
while(p) {
if(p & 1) ans = ans * a;
a = a * a;
p >>= 1;
}
return ans;
}
LL C[45][45], a[maxsz];
void process() {
for(int i = 0; i <= 40; i++) C[i][i] = C[i][0] = 1;
for(int i = 2; i <= 40; i++)
for(int j = 1; j < i; j++)
C[i][j] = add_mod(C[i-1][j-1], C[i-1][j]);
}
int main()
{
process();
scanf("%lld%d", &n, &k);
sz = k * 2 + 3;
for(int i = 0; i <= k; i++) {
a[i] = 1;
a[i + k + 1] = ((1LL << (i + 1)) % MOD);
}
a[sz - 1] = 1;
Matrix M;
for(int i = 0; i <= k; i++) M.a[i][i + k + 1] = 1;
for(int i = 0; i <= k; i++)
for(int j = 0; j <= i; j++) {
M.a[i+k+1][j+k+1] = C[i][j];
M.a[i+k+1][j] = mul(C[i][j], ((1LL << (i - j)) % MOD));
}
M.a[sz-1][sz-2] = M.a[sz-1][sz-1] = 1;
M = pow_mod(M, n - 1);
LL ans = 0;
for(int i = 0; i < sz; i++)
add(ans, mul(M.a[sz-1][i], a[i]));
printf("%lld\n", ans);
return 0;
}
CodeForces 392C Yet Another Number Sequence 矩阵快速幂的更多相关文章
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- 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 ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
随机推荐
- c# ExpandoObject动态扩展对象
js中的Object 对象. php中的stdClass. c# 也有动态可扩展对象 ExpandoObject,需要添加System.Dynamic引用 用法: dynamic model = ne ...
- feign容断忽略某些异常
@HystrixCommand(ignoreExceptions={ BusinessException.class, IllegalArgumentException.class, BadCrede ...
- java中循环的不同终止方式
1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...
- javascript中两种基本常用排序算法分析
备注:内容大部分从网上复制,代码为自己手写.仅做知识的温故知新,并非原创. 1.冒泡排序(Bubble Sort) (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两 ...
- hadoop完全分布式模式搭建和hive安装
简介 Hadoop是用来处理大数据集合的分布式存储计算基础架构.可以使用一种简单的编程模式,通过多台计算机构成的集群,分布式处理大数据集.hadoop作为底层,其生态环境很丰富. hadoop基础包括 ...
- myeclipes出现{Could not create the view: An unexpected except
今天编写代码的时候突然出现了web工程不能部署的情况,后面了解到主要是因为那个myeclipse非正常关闭或者突然断电的情况,我的是属于第一种的,下面整理一下这个解决方法 1.关闭myeclipse ...
- Linq语法学习_增删篇。
关键词: select from where in into join on equals orderby descending thenby Table<TEntity> Default ...
- 【UML】对象图Object diagram(转)
http://blog.csdn.net/sds15732622190/article/details/48894751 前言 今天要说的是UML中的对象图.他与类图,合作图都有关系,是类图的实例化. ...
- Netbackup8.0以上版本,服务端生成证书,客户端获取、更新证书方式(整理中)
创建重发令牌 如果非主控主机已在主服务器上注册但其基于主机ID的证书不再有效,则可以重新颁发基于主机ID的证书.例如,证书在过期,被撤销或丢失时无效. 重发令牌是一种可用于重新颁发证书的令牌.它是一种 ...
- 如何使用Python生成200个优惠券(激活码)
解析: 常见的优惠券(激活码)是由数字.字母(大小写)组成: string.ascii_letters 26个大小写字母: string.digits 0-9数字: 随机组合 使用random.s ...