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 n, k (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
1 1
1
4 1
34
5 2
316
7 4
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;
}
CodeForces - 393E Yet Another Number Sequence的更多相关文章
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
- 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\). ...
- 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)
Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence
Number Sequence A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...
- [AX]AX2012 Number sequence framework :(三)再谈Number sequence
AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
随机推荐
- GoF23种设计模式之行为型模式之备忘录模式
一.概述 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象的外部保存这个状态.以便以后可以将该对象恢复到原先保存的状态. 二.适用性 1.当需要保存一个对象在某个时刻的状态( ...
- poj-2386 lake counting(搜索题)
Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...
- 二叉排序树:HDU3791-二叉搜索树(用指针建立二叉排序树)
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- LAMP动态网站安装脚本
#!/bin/bash #auto make install LAMP #by authors zhangjianghua #httpd define path variable H_FILES=ht ...
- JSP 页面 jstl 时间戳 long型转时间
转载http://www.cnblogs.com/gmq-sh/p/5528989.html
- dubbo rpc filter实现剖析(一)
2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的filter的实现,包括作用,用法,原理,与Spring Cloud在这些能力的对比. 共提供了多少个?是哪些?发布时默认装配 ...
- rocketmq源码分析4-事务消息实现原理
为什么消息要具备事务能力 参见还是比较清晰的.简单的说 就是在你业务逻辑过程中,需要发送一条消息给订阅消息的人,但是期望是 此逻辑过程完全成功完成之后才能使订阅者收到消息.业务逻辑过程 假设是这样的: ...
- python - 目录处理
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_文件目录操作.py@ide: PyCharm Community ...
- Linux Shell系列教程之(四)Shell注释
本文是Linux Shell系列教程的第(四)篇,更多shell教程请看:Linux Shell系列教程 与许多的编程语言一样,Shell中也有注释符号,今天就为大家来介绍下Shell中的注释的语法及 ...
- Bind Enum To DropDownList ?
0. 准备测试数据 0.1 : 枚举 using System; using System.Collections.Generic; using System.Linq; using System.W ...