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

Examples

Input

1 1

Output

1

Input

4 1

Output

34

Input

5 2

Output

316

Input

7 4

Output

73825

之前做过一道 需要求 f[i] * i 生成函数形式的题,在那道题的题解里(http://www.cnblogs.com/JYYHH/p/8822572.html)已经证明过了这个玩意的生成函数的分母是 (1-x-x^2)^2 。。

当然,更普遍的,我们可以证明 f[i] * i^k 的生成函数表示的分母是 (1-x-x^2)^(k+1) ,这个用二项式定理解一下多项式闭形式就ojbk了。    于是我们可以得到这个函数的递推式,于是就可以直接预处理出前若干项然后直接用矩阵做了。

你问我它还要求前缀和????  这个是矩阵的常规操作啊23333
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=93;
const int ha=1000000007;
int K,A[maxn],n,F[maxn];
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an;}
struct node{
	int a[maxn][maxn];
	inline void clear(){ memset(a,0,sizeof(a));}
	inline void BASE(){ clear(); for(int i=n+1;i;i--) a[i][i]=1;}
	node operator *(const node &u)const{
		node r; r.clear();
		for(int k=n+1;k;k--)
		    for(int i=n+1;i;i--)
		        for(int j=n+1;j;j--) r.a[i][j]=add(r.a[i][j],a[i][k]*(ll)u.a[k][j]%ha);
		return r;
	}
}X,ANS;
ll N;

inline void init(){
	n=2,A[0]=1,A[1]=A[2]=ha-1;
	for(int i=1;i<=K;i++){
		for(int j=n;j>=0;j--){
			A[j+2]=add(A[j+2],ha-A[j]);
			A[j+1]=add(A[j+1],ha-A[j]);
		}
		n+=2;
	}
	for(int i=1;i<=n;i++) A[i]=ha-A[i];

	F[0]=F[1]=1;
	for(int i=2;i<=n;i++) F[i]=add(F[i-1],F[i-2]);
	for(int i=1;i<=n;i++) F[i]=F[i]*(ll)ksm(i,K)%ha;
}

inline void build(){
	X.clear(),ANS.BASE();
	for(int i=1;i<n;i++) X.a[i][i+1]=1;
	for(int i=1;i<=n;i++) X.a[i][1]=X.a[i][n+1]=A[i];
	X.a[n+1][n+1]=1;
}

inline int calc(){
	int ans=0,cnt=0;
	if(N<=n) for(int i=1;i<=N;i++) ans=add(ans,F[i]);
	else{
		N-=n,memset(A,0,sizeof(A));
		for(int i=1;i<=n;i++){
			A[i]=F[n-i+1];
			A[n+1]=add(A[n+1],F[i]);
		}

		for(;N;N>>=1,X=X*X) if(N&1) ANS=ANS*X;

		for(int i=n+1;i;i--) ans=add(ans,A[i]*(ll)ANS.a[i][n+1]%ha);
	}
	return ans;
}

inline void solve(){
    cin>>N>>K;
	init(),build();
	printf("%d\n",calc());
}

int main(){
	solve();
	return 0;
}

  

 

原文地址:https://www.cnblogs.com/JYYHH/p/8869738.html

时间: 2024-10-08 13:29:41

CodeForces - 393E 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 - 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(

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&#39;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

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i