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 - ...
随机推荐
- JQuery-- 获取元素的宽高、获取浏览器的宽高和垂直滚动距离
* 能够使用jQuery设置尺寸 * .width() width * .innerWidth() width + padding * .outerWidth() width + padding + ...
- httpclient向浏览器发送get和post请求
get请求代码实现 public static void main(String[] args) { CloseableHttpClient httpClient = null; //请求对象 Cl ...
- SQL Server 记录(更新中...)
sys.databases 显示所有数据库信息 sys.tables 显示当前数据库所有的表的信息 Go 向 SQL Server 实用工具发出一批 Transact-SQL 语句已结束的信号,Go本 ...
- SQLServer —— 用户权限操作
说明 以下操作都是基于SQLServer登陆验证方式登陆.而且操作员都是 sa. 一.添加登陆账号 use master go ' 第一个(xu)是登陆名,第二个(123456)是登陆密码. 执行语句 ...
- hdu 1272 使用set和并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1272 这道题就是求图是不是连通无环,我觉得其实就是看看图是不是一棵最小生成树. 所以要是图满足条件,就必然有n个 ...
- FastAdmin CMS 内容管理插件标签文档
FastAdmin CMS 内容管理插件标签文档 在CMS插件中的前端视图模板中有大量使用了自定义标签,我们在修改或制作模板的时候可以方便快捷的使用自定义标签来调用我们相关的数据. 标签库位于/add ...
- Linus 本尊也来了!为什么 KubeCon 越来越火了?
2015年11月,第一届 KubeCon 在美国旧金山开始的时候,还只是个200人的小会议,2019年的7月,KubeCon 第二次在中国举办,就有 3500 多位云原生和开源领域工程师齐聚一堂. 连 ...
- Nuxt.js打造旅游网站第2篇_首页开发
页面效果: 1.初始化默认布局 nuxtjs提供了一个公共布局组件layouts/default.vue,该布局组件默认作用于所有页面,所以我们可以在这里加上一些公共样式,在下一小结中还会导入公共组件 ...
- kubernetes1.3:操作Docker
Kubernetes对Docker的管理是通过一个第三方组件实现的.在Kubernetes1.2中这个第三方组件就是go-dockerclient,这是一个GO语言写的docker客户端,支持Dock ...
- 用GitHub Pages搭了个博客,欢迎来玩~
Welcome to visit my new blog https://luoxiaolei.github.io/ Ps. 后续的blog会优先更新到GitHub Pages上.