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 ...
随机推荐
- 一致性哈希算法(consistent hashing)【转】
一致性哈希算法 来自:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希 ...
- UNIX环境高级编程笔记之进程环境
本章讲的都是一些非常基础的知识,目的是为了下一章讲进程控制做铺垫,所以,本章就不做过多的总结了,直接看图吧.
- HTML CSS——margin和padding的学习
你在学习margin和padding的时候是不是懵了,——什么他娘的内边距,什么他娘的外边距.呵呵呵,刚开始我也有点不理解,后来通过查资料学习总算弄明白了,现在我来谈一下自己对margin和paddi ...
- ps图像渐变
整理自:http://zhidao.baidu.com/question/16374167.html 1.用ps打开图片 2.在切换至英文输入法状态下(下面的操作均如此)按q 快捷键q的作用是切换标准 ...
- Web Component--01. 简介
Web Components 是什么? Web Components是W3C定义的新标准,它给了前端开发者扩展浏览器标签的能力,可以自由的定制组件,更好的进行模块化开发,彻底解放了前端开发者的生产力. ...
- WCF安全1-开篇
概述: WCF安全简介 1.在企业级应用中什么是“安全” 答: (1)应用能够识别用户的身份-认证Authentication (2)应用能够将用户的操作和可访问的资源限制在其允许的权限范围之内-授权 ...
- DateTimePicker 控件的格式设置
DateTimePicker 控件的格式设置 CustomFormat属性设置 : yyyy-MM-dd HH:mm:ss 月大写M,分钟小写m,小时H代表24小时计算,h代表12小时计算yyyy- ...
- DDD:再谈:实体能否处于非法状态?
背景 实体能否处于非法状态吗?如果实体只承担其作为实体的职责,我不认为实体可以处于非法状态,如果您将实体在不同的分层之间传递,如:UI->Application->Domain-Data, ...
- 火狐浏览器修改userAgent
火狐浏览器修改userAgent的办法: 在火狐浏览器地址栏输入“about:config”,按下回车进入设置菜单. 找到“general.useragent.override”,如果没有这一项,则点 ...
- 如何拿到国内IT巨头的Offer
感觉写的很真实,分享一下.原文链接:http://jingyan.baidu.com/article/72ee561aa16d23e16138df3d.html 不久前,byvoid面阿里星计划的面试 ...