SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)
Problem description.
The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1.
We define a function as follows D(n,x) = x + x^2 + 2x^3 + 3x^4 + 5x^5 + 8x^6 +...+f(n)x^n
Given two integers n and x, you need to compute D(n,x) since the output can be very large output the result modulo 1000000007 (1e9+7) .
Input
Input description.
- The first line of the input contains an integer T denoting the number of test cases.
The description of T test cases follows. - The first line of each test case contains two integers n and x as described above.
Output
Output description.
- For each test case, output D(n,x)%1000000007 in a seperate line.
Constraints
Should contain all the constraints on the input data that you may have. Format it like:
- 1 ≤ T ≤ 1000
- 0 ≤ n ≤ 10^15
- 0 ≤ x ≤ 10^15
Example
Input:
1
7 11 Output:
268357683
题意:f(n)是斐波拉契数列,g(n)=f(n)*x^n;求前N项的g(n)的累加和。
思路:易得g(n)=g(n-1)*x+g(n-2)*x^2,
可以得到g(n)的矩阵求解方程: g(n)=base^N*g(0); 其中,base[1][1]=X; base[1][2]=X^2; base[2][1]=1(由递推式得到);
前缀和可以由大矩阵得到:A[1][1]=base; A[1][2]=1; A[2][2]=1(有前缀和求和公式得到) ;
大概地解释了一下,不是很清楚,可以看代码。前缀和可以参考这里:http://www.cnblogs.com/hua-dong/p/8479103.html
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int Mod=1e9+;struct mat
{
ll mp[][];
mat(){memset(mp,,sizeof(mp)); }
mat friend operator *(mat a,mat b)
{
mat res;
for(int k=;k<=;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%Mod)%Mod;
return res;
}
mat friend operator ^(mat a,ll x)
{
mat res;
for(int i=;i<=;i++) res.mp[i][i]=;
while(x){
if(x&) res=res*a; a=a*a; x>>=;
} return res;
}
};
int main()
{
ll T,N,X;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld",&N,&X); X%=Mod;
mat base;
base.mp[][]=X; base.mp[][]=X*X%Mod; base.mp[][]=;
for(int i=;i<=;i++) base.mp[i][i+]=base.mp[i+][i+]=;
base=base^N;
printf("%lld\n",base.mp[][]*X%Mod);
}
return ;
}
SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)的更多相关文章
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 . 设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...
- Codeforces1065G Fibonacci Suffix 【递推】【二分答案】
题目分析: 首先为了简便起见我们把前$15$的答案找出来,免得我们还要特判$200$以内之类的麻烦事. 然后我们从$16$开始递推.考虑猜测第i位是$0$还是$1$(这本质上是个二分).一开始先猜是$ ...
- POJ 3734 Blocks 矩阵递推
POJ3734 比较简单的递推题目,只需要记录当前两种颜色均为偶数, 只有一种颜色为偶数 两种颜色都为奇数 三个数量即可,递推方程相信大家可以导出. 最后来个快速幂加速即可. #include< ...
- Number String(HDU 4055,动态规划递推,前缀和优化)
点击加号查看代码 #include<bits/stdc++.h>//前缀和优化版本,不易理解 using namespace std; #define ll long long ; ; l ...
- CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...
- Luogu 1962 斐波那契数列(矩阵,递推)
Luogu 1962 斐波那契数列(矩阵,递推) Description 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1) = 1 f(2) = 1 f(n) = f(n-1) + f(n ...
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- 51nod1149 Pi的递推式
基准时间限制:1 秒 空间限制:131072 KB 分值: 640 F(x) = 1 (0 <= x < 4) F(x) = F(x - 1) + F(x - pi) (4 <= x ...
随机推荐
- sql通配符+sql中查询条件包含下划线等通配符的写法
一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...
- HDU1272 迷宫通路数
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- [POJ3041] Asteroids(最小点覆盖-匈牙利算法)
传送门 题意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍,最少要几次. 解析: 把每一行与每一列当做二分图两边的点. 某格子有障碍,则对应行与列连边. ...
- hdu 1043 A*
http://www.cnblogs.com/183zyz/archive/2011/08/12/2135827.html #include<stdio.h> #define N 3630 ...
- 钓鱼(洛谷 P1717)
题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次“年年大丰收”,为了表示诚意,他还决定亲自去钓鱼,但是,因为还要准备2013NO ...
- CF723E(欧拉回路)
题意: 给出一个有向图,要求给每条边重定向,使得定向后出度等于入度的点最多,输出答案和任意一种方案. 分析: 将图看作无向图,对每条边重定向 首先我们肯定分成多个连通分量来考虑,每一个连通分量都是一个 ...
- linux signal 列表
Linux 信号表 Linux支持POSIX标准信号和实时信号.下面给出Linux Signal的简表,详细细节可以查看man 7 signal. 默认动作的含义如下: Term 终止进程 ...
- Unable to process request: General SSLEngine problem.Unable to connect to neo4j at `localhost:7687`, because the certificate the server uses has changed.
Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException: Unable to proce ...
- Spring中实现自定义事件
原理: 通过扩展ApplicationEvent,创建一个事件类CustomEvent.这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数. 一旦定义事件类, ...
- webservice0基础
在学习webservice的时候,常常将ns和url花了好久时间才理解过来,这里备份下. 首先定义接口: @WebService public interface IService { @WebRes ...