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(矩阵递推&前缀和)的更多相关文章

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

  2. POJ 3734 Blocks(矩阵快速幂+矩阵递推式)

    题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 .   设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...

  3. Codeforces1065G Fibonacci Suffix 【递推】【二分答案】

    题目分析: 首先为了简便起见我们把前$15$的答案找出来,免得我们还要特判$200$以内之类的麻烦事. 然后我们从$16$开始递推.考虑猜测第i位是$0$还是$1$(这本质上是个二分).一开始先猜是$ ...

  4. POJ 3734 Blocks 矩阵递推

    POJ3734 比较简单的递推题目,只需要记录当前两种颜色均为偶数, 只有一种颜色为偶数 两种颜色都为奇数 三个数量即可,递推方程相信大家可以导出. 最后来个快速幂加速即可. #include< ...

  5. Number String(HDU 4055,动态规划递推,前缀和优化)

    点击加号查看代码 #include<bits/stdc++.h>//前缀和优化版本,不易理解 using namespace std; #define ll long long ; ; l ...

  6. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  7. Luogu 1962 斐波那契数列(矩阵,递推)

    Luogu 1962 斐波那契数列(矩阵,递推) Description 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1) = 1 f(2) = 1 f(n) = f(n-1) + f(n ...

  8. hdu3306 Another kind of Fibonacci【矩阵快速幂】

    转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...

  9. 51nod1149 Pi的递推式

    基准时间限制:1 秒 空间限制:131072 KB 分值: 640 F(x) = 1 (0 <= x < 4) F(x) = F(x - 1) + F(x - pi) (4 <= x ...

随机推荐

  1. 内置函数--map,filter,reduce

    一.map class map(object): """ map(func, *iterables) --> map object Make an iterator ...

  2. 【CSS】position relative 用法

    Relative是position的一个属性,是相对定位. position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:stat ...

  3. 组队训练2 回放(转载至cxhscst2's blog)

    2017/3/4  12:00-17:00 Solve 9 / 13 Penalty 717 练习赛过程回放: 开场5分中J题签到(cst) 12分钟时qw签到A 这时qw继续开写M,WA,检查代码. ...

  4. Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  5. C++ Virtual 关键字

    虚函数是C++中用于多态的机制.核心理念就是通过基类访问派生类定义的函数. 基类的析构含糊都必须是virtual的 虚函数只能借助于指针或者引用来达到多态的效果. 前提B类继承与A类 且foo()为虚 ...

  6. 扫描仪共享工具(BlindScanner Pro) 3.23 特别版

    http://www.xdowns.com/soft/1/126/2014/Soft_125206.html

  7. nexus3.1私服搭建

    原文:http://blog.csdn.net/qqqqq210/article/details/52993337 1.简介 近期公司需要搭建jenkins自动化构建部署,需要搭建nexus私服环境, ...

  8. Manage, Administrate and Monitor GlassFish v3 from Java code usingAMX &amp; JMX

    http://kalali.me/manage-administrate-and-monitor-glassfish-v3-from-java-code-using-amx-jmx/ Manage, ...

  9. SpringBoot初始教程之日志处理(二)

    SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...

  10. Android点击Button水波纹效果

    先上图,看看接下来我要向大家介绍的是个什么东西,例如以下图: 接下来要介绍的就是怎样实现上述图中的波纹效果.这样的效果假设大家没有体验过的话,能够看看百度手机卫士或者360手机卫士,里面的按钮点击效果 ...