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. js数组&&字符串&&定时器1

    一.简单计算命令eval var str="6*5"; alert(eval(str)); 注意:不安全,一般都不会去用   二.id&&name id:只能唯一 ...

  2. web 电子商务网站开发笔记整理

    js只保留整数,向上取整,四舍五入,向下取整等函数 来源:ab蓝学网整理 时间:2014-07-09 点击:30131 简介:WEB前端|1.丢弃小数部分,保留整数部分parseInt(5/2)2.向 ...

  3. group by order by having

  4. c# winform实现网页上用户自动登陆,模拟网站登录

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  5. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

  6. SomeThing of Memcache

    Memcache for .net 文章一: http://blog.csdn.net/dinglang_2009/article/details/6917794 不定时更新

  7. SGU 239.Minesweeper

    题意: 如图,一列未知的区域长度为n(≤1000),给出第二列的数字,求区域中雷的排列有多少种. Solution: 搜索.这题看上去1000的范围很大,实际上加上合理的剪枝,状态数会变得非常非常少. ...

  8. Asp.net创建伪静态页面

    下面是我研究了好几天和同事一起才研究出来的,原创. 1伪静态的定义: 伪静态是相对真实静态来讲的,通常我们为了增强搜索引擎的友好面,都将文章内容生成静态页面,但是有的朋友为了实时的显示一些信息.或者还 ...

  9. xml之phpdom操作

    php xml编程XML解析技术介绍 1.php与DOM 2.PHP与XPath 3.SimpleXML DOM(document object model)文档对象模型 把一个文件看做一个对象模型, ...

  10. Win7 + VS2015 + CMake3.6.1-GUI编译库

    CMake生成Unicode版本VC工程 Just add this line in your top CMakeLists.txt file:     add_definitions(-DUNICO ...