【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)

时间:2023-03-08 15:49:33
【 CodeForces - 392C】  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 modulo1000000007 (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
【分析】

  哈哈照着上一题的方法我就弄出来了~~
  应该是形如 x^k的形式,x很大,k较小的时候可以用二项式定理展开,求递推式然后矩阵加速。。
  【 CodeForces - 392C】  Yet Another Number Sequence (二项式展开+矩阵加速)
【 CodeForces - 392C】  Yet Another Number Sequence (二项式展开+矩阵加速)   就这样,qpow n次就好啦~ 代码如下:
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Mod 1000000007
#define Maxn 110
#define LL long long struct node
{
LL a[Maxn][Maxn];
}t[]; LL c[Maxn][Maxn];
LL n,k; void init()
{
memset(c,,sizeof(c));
for(LL i=;i<=;i++) c[i][]=;
for(LL i=;i<=;i++)
for(LL j=;j<=;j++)
c[i][j]=(c[i-][j-]+c[i-][j])%Mod;
} void get_m()
{
for(LL i=k+;i<=*k+;i++)
{
for(LL j=;j<=i-k-;j++) t[].a[i][j]=c[i-k-][j];
for(LL j=i+;j<=*k+;j++) t[].a[i][j]=;
}
for(LL i=;i<=k;i++)
{
for(LL j=;j<=i;j++) t[].a[i][j]=t[].a[i][j+k+]=c[i][j];
for(LL j=i+;j<=k;j++) t[].a[i][j]=t[].a[i][j+k+]=;
t[].a[i][*k+]=;
}
for(LL i=;i<=*k+;i++) t[].a[*k+][i]=;
t[].a[*k+][*k+]=t[].a[*k+][k]=;
} void get_un()
{
memset(t[].a,,sizeof(t[].a));
for(LL i=;i<=*k+;i++) t[].a[i][i]=;
} void mul(LL x,LL y,LL z)
{
for(LL i=;i<=*k+;i++)
for(LL j=;j<=*k+;j++)
{
t[].a[i][j]=;
for(LL l=;l<=*k+;l++)
t[].a[i][j]=(t[].a[i][j]+t[y].a[i][l]*t[z].a[l][j])%Mod;
}
t[x]=t[];
} void qpow(LL b)
{
get_un();
while(b)
{
if(b&) mul(,,);
mul(,,);
b>>=;
}
} int main()
{
init();
scanf("%lld%lld",&n,&k);
get_m();
qpow(n);
LL ans=;
for(LL i=;i<*k+;i++) ans=(ans+t[].a[*k+][i])%Mod;
printf("%lld\n",ans);
return ;
}

a


2016-09-26 16:11:26