Maybe ACMers of HIT are always fond of fibonacci numbers, because it is so beautiful. Don't you think so? At the same time,fishcanfly always likes to change and this time he thinks about the following series of numbers which you can guess is derived from the definition of fibonacci number.

The definition of fibonacci number is:

f(0) = 0, f(1) = 1, and for n>=2, f(n) = f(n - 1) + f(n - 2)

We define the new series of numbers as below:

f(0) = a, f(1) = b, and for n>=2, f(n) = p*f(n - 1) + q*f(n - 2),where p and q are integers.

Just like the last time, we are interested in the sum of this series from the s-th element to the e-th element, that is, to calculate .""""

Great!Let's go!

Input

The first line of the input file contains a single integer t (1 <= t <= 30), the number of test cases, followed by the input data for each test case.

Each test case contains 6 integers a,b,p,q,s,e as concerned above. We know that -1000 <= a,b <= 1000,-10 <= p,q <= 10 and 0 <= s <= e <= 2147483647.

Output

One line for each test case, containing a single interger denoting S MOD (10^7) in the range [0,10^7) and the leading zeros should not be printed.

Sample Input

2
0 1 1 -1 0 3
0 1 1 1 2 3

Sample Output

2
3
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const long long mod=1e7; typedef struct
{
long long m[3][3];
}mat; mat I={1,0,0,0,1,0,0,0,1}; mat calc(mat a,mat b) //矩阵相乘计算
{
int i,j,k;
mat c;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
c.m[i][j]=0;
for(k=0;k<3;k++)
{
c.m[i][j]+=(a.m[i][k]*b.m[k][j]+mod)%mod;
}
c.m[i][j]=(c.m[i][j]+mod)%mod;
}
return c;
} mat matirx(mat P,long long n) //矩阵快速幂(二分法)
{
mat m=P,b=I;
while(n>=1)
{
if(n&1) b=calc(b,m);
n>>=1;
m=calc(m,m);
}
return b;
} int main()
{
int t,a,b,p,q;
long long s,e,sum;
cin>>t;
while(t--)
{
sum=0;
scanf("%d%d%d%d%lld%lld",&a,&b,&p,&q,&s,&e);
mat x,y,P={p,q,0,1,0,0,1,0,1}; //p,q由输入决定,不能在全局定义mat P
y=matirx(P,e);
sum=(b*y.m[2][0]+a*y.m[2][1]+a*y.m[2][2])%mod;
sum=(sum+mod)%mod;
if(s>1)
{
x=matirx(P,s-1);
sum=sum-(b*x.m[2][0]+a*x.m[2][1]+a*x.m[2][2])%mod;
sum=(sum+mod)%mod;
}
else if(s==1)
sum-=a;
sum=(sum+mod)%mod;
printf("%lld\n",sum);
}
return 0;
}

  

fibonacci数列的和取余(2)的更多相关文章

  1. fibonacci数列的和取余(1)

    As we know , the Fibonacci numbers are defined as follows:  """" Given two numbe ...

  2. Fibonacci数列(数列 取模)

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...

  3. ACM_无聊者序列(斐波那契数列大数取余(同余)+规律)

    Problem Description: 瓜瓜在玩着由红色和蓝色的大理石做成的玻璃珠,他将n个玻璃珠从左到右排成一个序列叫做无聊者序列.一个非空的红色和蓝色玻璃珠组成的序列是一个无聊者序列.这个序列的 ...

  4. Fibonacci数列对任何数取模都是一个周期数列

    题目是要求出斐波那契数列n项对一个正整数取模,那么可以把斐波那契数列取模后得到的数列周期求出来. 比如下面一个题目:求出f[n]的后4位,先求出数列对10000取模的周期,然后再查找即可. #incl ...

  5. Java实现Fibonacci取余

    Description Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. Input 多 ...

  6. 入门训练 Fibonacci数列

      入门训练 Fibonacci数列   时间限制:1.0s   内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...

  7. 蓝桥杯 入门训练 Fibonacci数列(水题,斐波那契数列)

    入门训练 Fibonacci数列 时间限制:1.0s   内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非 ...

  8. 蓝桥杯 入门训练 Fibonacci数列

      入门训练 Fibonacci数列   时间限制:1.0s   内存限制:256.0MB        问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. ...

  9. Fibonacci数列

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...

随机推荐

  1. Android Studio 经常使用功能介绍

    为了简化 Android 的开发力度,Google 决定将重点建设 Android Studio 工具.Google 会在今年年底停止支持其它集成开发环境.比方 Eclipse. Android St ...

  2. React 根据官方总结的规范

    1.语法上,根据生命周期方法执行的顺序编写代码 (1 生命周期方法[getDefaultProps, getInitialState, componentWillMount, componentDid ...

  3. List的遍历和删除元素

    package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...

  4. Bulk Insert的用法 .

    /******* 导出到excel */EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S&quo ...

  5. QT在windows下实现截屏操作并保存为png图片

    QPixmap originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); QString format = ...

  6. Unity不同平台生成中预处理的注意点

    http://blog.csdn.net/pandawuwyj/article/details/7959335 Unity3D的项目,这周吃亏在宏上了.大背景是项目需要在Unity中用Hudson自动 ...

  7. springMVC对于controller处理方法返回值的可选类型

    http://www.360doc.com/content/14/0309/19/834950_359081989.shtml

  8. MemSQL start[c]up Round 2 - online version C. More Reclamation(博弈)

    题目大意 额,写来写去,我还是直接说抽象之后的题目大意吧: 有一个 r*2 的矩形,两个人轮流的在矩形上面减去一个 1*1 的小正方形,要求在减的过程中,不能使矩形“断开”,也就是说,如果一个人减去了 ...

  9. html中css三种常见的样式选择器 zz

    1:标签选择器 标签选择器,是所有带有某种标签的都生效.这里以p为例,也就是所有的带有p标记的都会这样的样式 <html><head><styletype="t ...

  10. 二叉搜索树BinarySearchTree(C实现)

    头文件—————————————————————————————— #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #inc ...