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较小的时候可以用二项式定理展开,求递推式然后矩阵加速。。
  
  就这样,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

 

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

  3. 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  4. CodeForces - 393E Yet Another Number Sequence

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

  5. hdu3483之二项式展开+矩阵快速幂

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  6. Number Sequence(快速幂矩阵)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  7. 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)

    已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...

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

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

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. oracle ebs中并发程序定义查询sql

    ---concurrent program define SELECT FCPV.CONCURRENT_PROGRAM_ID, FCPV.CONCURRENT_PROGRAM_NAME, FCPV.U ...

  2. linux的nohup disown setsid screen

    ```nohup 可以将进程挂起 帐号退出时不受影响 nohup cp -r a b/ [>/~/cp.log] & setsid 作用类似于nohup swtsid cp -r a b ...

  3. Linux基础(二)

    二.Linux 常用命令 一.命令行操作的流程 录入命令(可以使用各种途径来发送命令) 命令被解释器解释并执行 将结果以产品需要的方式显示出来 二.命令提示符 sq@sq-VirtualBox:~$ ...

  4. Log4Net详细配置

    关于Log4Net配置主要分几步 第一步:下载log4net.dll(log4net官网:http://logging.apache.org/log4net/download_log4net.cgi) ...

  5. 20160330javaweb之session 小练习

    练习一:session 实现登录注销 package com.dzq.session.logout; import java.util.*; public class UserDao { /** * ...

  6. @Resource注解省略name属性后的行为

    @Resource有一个name属性,该属性值为所要注入的Bean实例的id,类似于<property.../>元素的ref属性,不过在spring中允许省略name属性值,省略后在以下情 ...

  7. C#中2、8、16进制 有符号转换10进制正负数

    曾经让我苦想的其他进制转有符号整型问题,结果自己想到方法解决后才发现原来如此简单. 1.Int16(2个byte长度 ) : 方法 :Convert.ToInt16(进制编码,进制) a.16进制转1 ...

  8. js和css分别实现透明度的动画实现

    js实现 两个函数 即setInterval和setTimeout setTimeout((function(){})(1/10),1*100) 该函数有两个参数,第一个为执行的函数,第二个为事件参数 ...

  9. Nginx的反向代理

    先通过简单的图来说明一下正向代理和反向代理吧~ 正向代理 代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介.刚开始的时候,代理多数是帮助内网client访问外网server用的(比 ...

  10. Hash函数的安全性

    我们为了保证消息的完整性,引进了散列函数,那么散列函数会对安全正造成什么影响呢?这是需要好好研究一番的问题. 三个概念: 1.如果y<>x,且h(x)=h(y),则称为碰撞. 2.对于给定 ...