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

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较小的时候可以用二项式定理展开,求递推式然后矩阵加速。。  
时间: 2024-07-31 23:29:24

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

[矩阵快速幂] CodeForces 392C Yet Another Number Sequence

题意: 题目意思很明朗~ 思路: A(n+1)=F(n+1)*(n+1)^k A(n)=F(n)*(n)^k A(n-1)=F(n-1)*(n-1)^k 这里拿k=2来举例 A(n+1)=F(n)*(n+1)^2+F(n-1)*(n+1)^2 对于A(n+1)发现可以由A(n)和A(n-1)得到 实际上就是多了 2*n+1个F(n) 和4*n个F(n-1) 其实就是n^k -> (n+1)^k  以及 (n-1)^k ->(n+1)^k 大家算一算就发现是和杨辉三角有关的,就是系数 这样我们就

CodeForces - 393E Yet Another Number Sequence

Discription 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 thi

Number Sequence(快速幂矩阵)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 131753    Accepted Submission(s): 31988 Problem Description A number sequence

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi'an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence