fibonacci数列的和取余(2)
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)的更多相关文章
- fibonacci数列的和取余(1)
As we know , the Fibonacci numbers are defined as follows: """" Given two numbe ...
- Fibonacci数列(数列 取模)
问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...
- ACM_无聊者序列(斐波那契数列大数取余(同余)+规律)
Problem Description: 瓜瓜在玩着由红色和蓝色的大理石做成的玻璃珠,他将n个玻璃珠从左到右排成一个序列叫做无聊者序列.一个非空的红色和蓝色玻璃珠组成的序列是一个无聊者序列.这个序列的 ...
- Fibonacci数列对任何数取模都是一个周期数列
题目是要求出斐波那契数列n项对一个正整数取模,那么可以把斐波那契数列取模后得到的数列周期求出来. 比如下面一个题目:求出f[n]的后4位,先求出数列对10000取模的周期,然后再查找即可. #incl ...
- Java实现Fibonacci取余
Description Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. Input 多 ...
- 入门训练 Fibonacci数列
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...
- 蓝桥杯 入门训练 Fibonacci数列(水题,斐波那契数列)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非 ...
- 蓝桥杯 入门训练 Fibonacci数列
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. ...
- Fibonacci数列
问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...
随机推荐
- Oracle 一次生产分库,升级,迁移
今天完成了一个负载较高的中央数据库的分库操作, 并实现了oracle的滚动升级(10.2.0.1->10.2.0.4), 业务中断仅15分钟. 平台: RHEL AS 4 + Oracle 10 ...
- 用git上传本地项目到github上
首先确认自己已经安装了git,打开git bash,输入ssh-keygen -t rsa -C "自己的邮箱地址@XXX.com" ,生成自己的公钥与私钥 一路默认回车,会生 ...
- Hadoop 2.4.1 Map/Reduce小结【原创】
看了下MapReduce的例子.再看了下Mapper和Reducer源码,理清了参数的意义,就o了. public class Mapper<KEYIN, VALUEIN, KEYOUT, VA ...
- asp.net首页设置
在web.config中设置首页 <configuration> <system.web> <compilation debug="true" tar ...
- NPM 与 left-pad 事件:我们是不是早已忘记该如何好好地编程?
转自:http://www.techug.com/npm-left-pad-have-we-forgotten-how-to-program 开发者朋友们,我们该谈一谈这个问题了.你们应该知道本周的 ...
- Ext JS 6 入门学习资料大全(2016-12-14)
现在 sencha touch已经升级为 Ext JS 6 了重新整理下资料 官方网站:https://www.sencha.com/ 在线文档:http://docs.sencha.com/extj ...
- Unix sed实用教程开篇
已经看了一段时间的Linux Shell编程了,也能完成一些基本的使用,为了加深理解,恰好看到了The Unix School的一个sed&awk教程,不是简单的命令参数堆积,而是一个相当实用 ...
- nginx 常用配置说明
一.location 配置 1.1 语法规则: location [=|~|~*|^~] /uri/ { … }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可 ...
- #ifdef 和 #if defined 的区别 -- 转
#ifdef 和 #if defined 的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB) xxxxxxxxx #e ...
- ubuntu 安装node.js + express + mongodb
转载自http://my.oschina.net/reg/blog/289536 Ctrl + Alt + T,打开终端,输入以下命令安装: sudo apt-get install nodejs 安 ...