http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=457
description |
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 S(n)=f(s)+f(s+1)+...+f(e);
|
input |
|
output |
|
sample_input |
|
sample_output |
|
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int N=3;
const int MOD=1e7;
struct Matrix
{
LL m[N][N];
};
Matrix I=//定义单位矩阵(快速幂中初始化变量时要用到)
{
1,0,0,
0,1,0,
0,0,1
};
Matrix multi(Matrix a,Matrix b)//两个矩阵相乘
{
Matrix c;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
c.m[i][j]=0;
for(int k=0; k<N; k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
}
c.m[i][j]%=MOD;
}
return c;
}
Matrix quick_mod(Matrix a,LL k)//矩阵快速幂(可做模板)
{
Matrix ans=I;
while(k!=0)
{
if(k&1)
{
ans=multi(ans,a);
}
k>>=1;
a=multi(a,a);
}
return ans;
}
int main()
{
LL a,b,p,q,s,e,s1,s2;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&p,&q,&s,&e);
Matrix A={1,p,q,
0,p,q,
0,1,0};
Matrix ans;
s--;
if(s==0)
s1=a;
else if(s<0)
s1=0;
else
{
ans=quick_mod(A,s-1);
s1=((ans.m[0][0]%MOD*(a+b)%MOD)%MOD+(ans.m[0][1]%MOD*b%MOD)%MOD+(ans.m[0][2]%MOD*a%MOD)%MOD)%MOD;//取我们要的矩阵中的元素,下同
}
if(e==0)
s2=a;
else
{
ans=quick_mod(A,e-1);
s2=((ans.m[0][0]%MOD*(a+b)%MOD)%MOD+(ans.m[0][1]%MOD*b%MOD)%MOD+(ans.m[0][2]%MOD*a%MOD)%MOD)%MOD;
}
LL t = ((s2 - s1) % MOD + MOD) % MOD;
printf("%lld\n",t);
}
return 0;
}