Description

  Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recurrence relation:

F1 = 1, F2 = 2, Fi = Fi - 1 + Fi - 2 (i > 2).

We'll define a new number sequence Ai(k) by the formula:

Ai(k) = Fi × ik (i ≥ 1).

In this problem, your task is to calculate the following sum: A1(k) + A2(k) + ... + An(k). The answer can be very large, so print it modulo 1000000007(109 + 7).

Input

  The first line contains two space-separated integers nk(1 ≤ n ≤ 1017; 1 ≤ k ≤ 40).

Output

  Print a single integer — the sum of the first n elements of the sequence Ai(k) modulo 1000000007(109 + 7).

Sample Input

Input
1 1
Output
1
Input
4 1
Output
34
Input
5 2
Output
316
Input
7 4
Output
73825

解题思路:
  这是一道矩阵快速幂求多项式的应用,思路很清晰:找到各个量之间的递推关系,用矩阵求幂转移状态。问题的关键在于推导A(n,k),sumA(n),
F(n)之间的关系。过程如下:
  1.令 U(n+1,k)=F(n+1)*(n+1)^k;
     V(n+1,k)=F(n)*(n+1)^k;
    Sum(n)=∑[i=1~n]A(i,k);
  2.利用二项式定理将(1+i)^n展开;
  则 U(n+1,k)=∑[i=0~k]C(i,k)*F(n)*(n)^i+∑[i=0~k]C(i,k)*F(n-1)*(n)^i

            =∑[i=0~k]C(i,k)*U(n,i)+∑[i=0~k]C(i,k)*V(n,i);
  同理 V(n+1,k)=
∑[i=0~k]C(i,k)*U(n,i);
  Sum(n)=Sum(n-1)+U(n,k);
  
  3.状态转移用矩阵向量表示:
sum(n-1)   sum(n)
U(n,k)     U(n+1,k)

...

  ...
U(n,0)      => U(n+1,0)
V(n,k)       V(n+1,k)

...

  ...
V(n,0)   V(n+1,0)
 
 
 




代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <time.h>
#include <assert.h>
#define time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
using namespace std;
#define maxk 40
#define MOD 1000000007
#define mod_(x) (x%MOD) typedef long long LL;
LL n;
int k; struct Matrix{
int row,col;
int m[*maxk+][*maxk+];
Matrix(int r=,int c=){
row=r;
col=c;
memset(m, , sizeof m);
}
void operator=(const Matrix& m2){
memcpy(m, m2.m, sizeof m);
}
};
Matrix operator*(const Matrix& a,const Matrix& b){
Matrix c(a.row,b.col);
for(int i=;i<c.row;i++)
for(int j=;j<c.col;j++){
for(int p=;p<a.col;p++){
c.m[i][j]=(c.m[i][j]+LL(a.m[i][p])*b.m[p][j])%MOD;
//assert(c.m[i][j]>=0);
}
}
return c;
}
Matrix C;
void set_C(){
for(int i=k;i>=;i--)
for(int j=k;j>=i;j--){
if(j==k||j==i)
C.m[i][j]=;
else
C.m[i][j]=(C.m[i+][j]+C.m[i+][j+])%MOD;
}
}
void cpy_C(Matrix& a,int pi,int pj){
int len=k+;
for(int i=;i<len;i++)
for(int j=;j<len;j++)
a.m[pi+i][pj+j]=C.m[i][j];
}
void set_M(Matrix& a){
a.m[][]=,a.m[][]=;
cpy_C(a, , );
cpy_C(a, k+, );
cpy_C(a, , k+);
}
Matrix pow_mod(Matrix& a,LL n){
if(n==) return a;
Matrix temp=pow_mod(a, n/);
temp=temp*temp;
if(n%){
temp=temp*a;
}
return temp;
}
int pow_2(int n){
if(n==) return ;
int temp=pow_2(n/)%MOD;
temp=int((LL(temp)*temp)%MOD);
if(n%)
temp=(temp*)%MOD;
return temp;
}
int main(int argc, const char * argv[]) {
while(scanf("%lld%d",&n,&k)==){
if(n==){
printf("%d\n",);
continue;
}
set_C();
Matrix I(*k+,*k+);
set_M(I);
Matrix A(*k+,);
A.m[][]=;
for(int i=;i<k+;i++)
A.m[i][]=pow_2(k+-i+);
for(int i=k+;i<*k+;i++)
A.m[i][]=pow_2(*k+-i);
Matrix temp=pow_mod(I, n-);
A=temp*A;
printf("%d\n",A.m[][]);
//time_;
}
return ;
}

Yet Another Number Sequence——[矩阵快速幂]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  8. 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\). ...

  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. ls command not found

    编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别. 在命令行下打入下面这段就可以了 export PATH=/usr/local/sbin:/usr/local/bin:/sbi ...

  2. 解决Apache日志"internal dummy connection"方法

    最近查看服务器中apache日志,发现有大量的 OPTIONS * HTTP/1.0" 200 - "-" "Apache (internal dummy co ...

  3. 2019-10-26-Inno-Setup-安装包脚本-Run-的-Flags-标记

    title author date CreateTime categories Inno Setup 安装包脚本 Run 的 Flags 标记 lindexi 2019-10-26 08:42:39 ...

  4. js cookies 的写入、读取、删除

    //写cookies //escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串.function setCookie(name,value) {     var Days ...

  5. 封装好的MySQL.class.php类

    封装好的MySQL.class.php类 作用:数据库操作类 <?php header('content-type:text/html;charset=utf-8'); class MySQLD ...

  6. 【JZOJ4835】【GDOI2017模拟10.31】量化交易

    题目描述 数据范围 解法 贪心: 从左往右枚举,设枚举到元素为x,并维护一个堆: 设此时堆顶元素为y, 如果x大于y,那么x可以与y产生差价,立即将差价贡献给答案. 如果y之前已经和其他元素z产生过差 ...

  7. phpcms多站点表单统一到主站点管理的解决方案

    1.在主站点新建子站点的表单向导,与子站点的设置保持一致 2.在各个子站点的数据库的表单数据表添加一个写入触发器,将新增的表单数据同步到主站点的数据库对应表里,这样主站点就能展示所有站点的表单数据 3 ...

  8. Kubernetes1.4新特性前瞻:设置JOB执行计划

    (一)  核心概念 Kubernetes在新版中会新增了一个设置JOB执行计划的功能,在1.3中已经可以初见端倪了,从进度上来看会在1.4版本中进行发布,下面我们先睹为快. Kubernetes通过这 ...

  9. ELK2之ELK的语法学习

    1.回顾 (1)es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,提供简单的RESTful API来隐藏Lucene的复杂性. es除了全文搜索引擎之外,还可以这样描述它 ...

  10. Xcode无法退出,报错提示 The document “xxx.h” could not be saved. The file doesn’t exist.

    记录一个问题 场景:Xcode编辑一个工程时直接在工程内部修改了某个目录的文件夹名字,而后删除了其下的某 .h.m 文件 之后总是提示上述错误且无法强制退出Xcode,clean等操作基本没用 查找本 ...