题意:

\(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 矩阵快速幂的更多相关文章

  1. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  2. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  3. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  4. 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 ...

  5. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  6. 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 ...

  7. 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 ...

  8. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  9. LightOJ 1065 - Number Sequence 矩阵快速幂水题

    http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...

随机推荐

  1. P3930 SAC E#1 - 一道大水题 Knight

    TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...

  2. columns分栏与flex弹性盒模型

    columns  分栏 值:column-width:设置每列的宽度        column-count:设置列数   例:columns{200px 3}   列数和宽度固定        co ...

  3. 常用快捷键—Webstorm

    常用快捷键—Webstorm入门指南 提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. 快捷键配置 点击“File”-> “setti ...

  4. 数据库操作----找了MySQL和SQL Sever两个的基础语句

    这是MySQL的基本操作: 1 登入数据库:mysql -uroot -p+密码 (SQL Sever登入: osql -U 用户名 -P 密码) 显示已存在的数据库:show databases; ...

  5. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:2.技术简介之MinaFilter(1)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Filter filter:过滤器?(不知道是不是这么翻译,算了知道意思就好了╮(╯▽╰)╭),这种东西在很多语言中 ...

  6. 纪念Google Reader—Google Reader的最后一天

    从2006年到今天,几乎每天我都会打开Google Reader,但是今天不一样,因为它是最后一天.心情有些依依不舍,像是与一位多年老朋友永别.因此我非常痛恨Google,先给你送来个好朋友,再从你身 ...

  7. servlet config

    <webapp> <!--servlet是指编写的Servlet的路径,以及定义别名--> <servlet> <servlet-name>test&l ...

  8. Ecshop数据表结构

    -- 表的结构 `ecs_account_log`CREATE TABLE IF NOT EXISTS `ecs_account_log` (`log_id` mediumint(8) unsigne ...

  9. python3操作mysql数据库表01(基本操作)

    #!/usr/bin/env python# -*- coding:UTF-8 -*- import requestsfrom bs4 import BeautifulSoupfrom bs4 imp ...

  10. C# 使用解析json 嵌套方法

    C#从网页不传参数 接收json数据 public String GetHtmlFromUrl(String url) { //Response.Write(url); //Response.End( ...