Yet Another Number Sequence——[矩阵快速幂]
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 n, k(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
1 1
1
4 1
34
5 2
316
7 4
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——[矩阵快速幂]的更多相关文章
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- 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 ...
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
- 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\). ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
随机推荐
- 【心有猛虎】react-pxq
这是一个比较完整的简单的react移动端项目,说起来页面少,其实,构思若是精巧,也并不容易做 先放源码:https://github.com/bailicangdu/react-pxq 接下来我们简单 ...
- golang之数据结构
4种:bool/int/uint/uintptr(其中bool类型的零值为false,其余类型的零值为0) 4种:float32/float64/complex64/complex126 (零值为0) ...
- Kubernetes Ingress 日志分析与监控的最佳实践
摘要: Ingress主要提供HTTP层(7层)路由功能,是目前K8s中HTTP/HTTPS服务的主流暴露方式.为简化广大用户对于Ingress日志分析与监控的门槛,阿里云容器服务和日志服务将Ingr ...
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
[链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /* 有一个要 ...
- P1127
题目描述 如果单词X的末字母与单词Y的首字母相同,则X与Y可以相连成X.Y.(注意:X.Y之间是英文的句号“.”).例如,单词dog与单词gopher,则dog与gopher可以相连成dog.goph ...
- 操作系统之LRU算法 C语言链表实现
LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历 ...
- 两篇论文之CNN中正交操作
CNN的权值正交性和特征正交性,在一定程度上是和特征表达的差异性存在一定联系的. 下面两篇论文,一篇是在训练中对权值添加正交正则提高训练稳定性,一篇是对特征添加正交性的损失抑制过拟合. 第一篇:Ort ...
- python系列之(5)PyMySQL的使用
简介 PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb. 安装 pip3 install pymysql 创建连接 #!/usr ...
- phpcms信息模型使用
PHPCMS V9 分类信息模型测试版下载 安装 1.确定您的phpcms版本为20110318版本以上 2.将所有文件覆盖到网站目录下,运行install_info. 3.将文件中所有的phpcms ...
- [Linux]环境配置之jdk的安装 标签: jdk服务器linux 2016-08-07 22:18 502人阅读 评论(21)
这两天服务器崩了,所以需要重新配置环境,然后从头到尾配置了一遍,现在记录总结一下自己这两天的工作,首先是jdk的配置! 很多软件,需要jdk为基础,所以第一个装的就是jdk. 第一步,拷贝文件 首先将 ...