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 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).
Sample Input
1 1
1
4 1
34
5 2
316
7 4
73825 解题思路:
这是一道矩阵快速幂求多项式的应用,思路很清晰:找到各个量之间的递推关系,用矩阵求幂转移状态。问题的关键在于推导A(n,k),sumA(n),
F(n)之间的关系。过程如下:
1.令 U(n+1,k)=F(n+1)*(n+1)^k;
V(n+1,k)=F(n)*(n+1)^k;
Sum(n)=∑[i=1~n]A(i,k);
2.利用二项式定理将(1+i)^n展开;
则 U(n+1,k)=∑[i=0~k]C(i,k)*F(n)*(n)^i+∑[i=0~k]C(i,k)*F(n-1)*(n)^i
=∑[i=0~k]C(i,k)*U(n,i)+∑[i=0~k]C(i,k)*V(n,i);
同理 V(n+1,k)=∑[i=0~k]C(i,k)*U(n,i);
Sum(n)=Sum(n-1)+U(n,k);
3.状态转移用矩阵向量表示:
| sum(n-1) | sum(n) | |
| U(n,k) | U(n+1,k) | |
|
... |
... | |
| U(n,0) | => | U(n+1,0) |
| V(n,k) | V(n+1,k) | |
|
... |
... | |
| V(n,0) | V(n+1,0) |
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <time.h>
#include <assert.h>
#define time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
using namespace std;
#define maxk 40
#define MOD 1000000007
#define mod_(x) (x%MOD) typedef long long LL;
LL n;
int k; struct Matrix{
int row,col;
int m[*maxk+][*maxk+];
Matrix(int r=,int c=){
row=r;
col=c;
memset(m, , sizeof m);
}
void operator=(const Matrix& m2){
memcpy(m, m2.m, sizeof m);
}
};
Matrix operator*(const Matrix& a,const Matrix& b){
Matrix c(a.row,b.col);
for(int i=;i<c.row;i++)
for(int j=;j<c.col;j++){
for(int p=;p<a.col;p++){
c.m[i][j]=(c.m[i][j]+LL(a.m[i][p])*b.m[p][j])%MOD;
//assert(c.m[i][j]>=0);
}
}
return c;
}
Matrix C;
void set_C(){
for(int i=k;i>=;i--)
for(int j=k;j>=i;j--){
if(j==k||j==i)
C.m[i][j]=;
else
C.m[i][j]=(C.m[i+][j]+C.m[i+][j+])%MOD;
}
}
void cpy_C(Matrix& a,int pi,int pj){
int len=k+;
for(int i=;i<len;i++)
for(int j=;j<len;j++)
a.m[pi+i][pj+j]=C.m[i][j];
}
void set_M(Matrix& a){
a.m[][]=,a.m[][]=;
cpy_C(a, , );
cpy_C(a, k+, );
cpy_C(a, , k+);
}
Matrix pow_mod(Matrix& a,LL n){
if(n==) return a;
Matrix temp=pow_mod(a, n/);
temp=temp*temp;
if(n%){
temp=temp*a;
}
return temp;
}
int pow_2(int n){
if(n==) return ;
int temp=pow_2(n/)%MOD;
temp=int((LL(temp)*temp)%MOD);
if(n%)
temp=(temp*)%MOD;
return temp;
}
int main(int argc, const char * argv[]) {
while(scanf("%lld%d",&n,&k)==){
if(n==){
printf("%d\n",);
continue;
}
set_C();
Matrix I(*k+,*k+);
set_M(I);
Matrix A(*k+,);
A.m[][]=;
for(int i=;i<k+;i++)
A.m[i][]=pow_2(k+-i+);
for(int i=k+;i<*k+;i++)
A.m[i][]=pow_2(*k+-i);
Matrix temp=pow_mod(I, n-);
A=temp*A;
printf("%d\n",A.m[][]);
//time_;
}
return ;
}
Yet Another Number Sequence——[矩阵快速幂]的更多相关文章
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- Yet another Number Sequence 矩阵快速幂
Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- 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\). ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
随机推荐
- Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
- echarts radar 一些会用到了的功能记录
点击雷达图小标题进行相应操作 // options中添加,坐标轴的标签是否响应和触发鼠标事件,默认不响应. radar:{ triggerEvent: true } // 添加监听事件, 点击雷达图标 ...
- JavaScript--淘宝图片切换
css样式有点问题,但是主要是js逻辑: <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...
- Java排序需掌握算法 详解
package com.sxt.review; /*内部排序:(在内存) * 插入排序-->希尔排序 * 冒泡排序-->快速排序 * 选择排序-->堆排序 * 归并排序 * 基数排序 ...
- C++五:重载 多态
C++五:重载与多态 一:概述 多态是指同样的消息被不同类型的对象接收导致不同的行为,即接口的多种不同的实现方式.多态可分为静态多态与动态多态.多态类型可分为四类:重载多态,强制多态,包含多态,参 ...
- Java练习 SDUT-1588_圆的面积
圆的面积 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description Give you the radius of a circle ...
- Java练习 SDUT-1184_拍皮球
C语言实验--拍皮球 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小瑜3岁了,很喜欢玩皮球,看来今后喜欢打篮球的^_ ...
- 根据花瓶的侧面投影图,用Matlab绘制花瓶的三维立体图
现有一花瓶侧面投影如图 问题: 1) 做出该花瓶三维立体图: 2) 计算其表面积: 计算其体积. 第一次参加数学建模,从来没有接触过Matlab语言,一上来就碰到这种数字图像处理的问题就 ...
- 20172018-acmicpc-southeastern-european-regional-programming-contest-seerc-2017-en A - Concerts
题意就是给一个字母序列a,以及一个另外一个字母序列b,你需要b中找到字母序列a,并且要求对于在b中的字母序列a,每个单词都需要满足相应的距离 其实很简单,我们利用DP[i][j]代表a已经匹配i个位置 ...
- mybatis-generator1.3.6的使用
下载地址: http://blog.mybatis.org/2017/12/mybatis-generator-version-136-released.html 参考了 http://blog.cs ...