HITOJ 2255 類似Fibonacci數列求和取模擴充
http://acm.hit.edu.cn/hoj/problem/view?id=2255
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題目大意:求出數列第a項到第b項和取模
解題思路:構造一個3*3的矩陣,利用矩陣連乘(注意邊界處理,和取模出現負數的情況)
/*This Code is Submitted by life4711 for Problem 2255 at 2014-07-25 11:59:58*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int N=3;
const LL MOD=10000000;
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]=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,q,p;
LL s,e;
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
};
if(s==0&&e==0)
{
if(a<0)
a+=MOD;
printf("%lld\n",a);
}
else if(s==0)
{
Matrix n=quick_mod(A,e-1);
LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
LL s=s2%MOD;
if(s<0)
s+=MOD;
printf("%lld\n",s);
}
else if(s==1)
{
Matrix n=quick_mod(A,e-1);
LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
LL s=(s2-a)%MOD;
if(s<0)
s+=MOD;
printf("%lld\n",s);
}
else
{
Matrix n=quick_mod(A,s-2);
LL s1=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
n=quick_mod(A,e-1);
LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
LL s=(s2-s1)%MOD;
if(s<0)
s+=MOD;
printf("%lld\n",s);
}
}
return 0;
}
相關文章
- HITOJ 2060 類似斐波那契數列(一段和取模)
- HITOJ 1864 求Fibonacci數列的位數
- PowerToys外掛擴充套件(類似Alfred)套件Alfred
- HDU 3059 Fibonacci數列與矩陣求和 矩陣大小不固定矩陣
- 整數取模類
- c#擴充套件方法奇思妙用變態篇一:由Fibonacci數列引出“委託擴充套件”及“遞推遞迴委託”C#套件遞迴
- C4top-N個數求和 (分數求和模擬)
- C#DateTime類擴充套件——獲取旬、季、年天數方法C#套件
- 轉向Kotlin——列舉類和擴充套件Kotlin套件
- Swift列舉,結構體,類,擴充套件,協議Swift結構體套件協議
- C# Enum列舉型別操作擴充套件類C#型別套件
- 分類擴充套件套件
- CONNECT BY 擴充套件用法,實現獲取bom級聯擴充套件數量套件
- HttpContext擴充套件類HTTPContext套件
- 數列求和【線段樹基礎】
- JMeter 擴充套件開發:擴充套件 TCP 取樣器JMeter套件TCP
- C#列舉(一)使用總結以及擴充套件類分享C#套件
- 隨機數擴充隨機
- repeat和tile擴充陣列陣列
- 一維陣列:相鄰兩數求和陣列
- 隔列求和
- 藍橋杯:入門訓練 Fibonacci數列
- [擴充套件推薦] 使用 laravel-gridCaptcha 本地生成類似於谷歌點圖驗證碼套件LaravelAPT谷歌
- es6-陣列擴充套件陣列套件
- D 區間求和 [數學 樹狀陣列]陣列
- Spring Boot中如何擴充套件XML請求和響應的支援Spring Boot套件XML
- C# 反射呼叫擴充類方法C#反射
- weex ios擴充套件類的作用iOS套件
- Java-IoUtil擴充套件工具類Java套件
- Laravel MongoDB 資料庫查詢擴充外掛 擴充原始 Laravel 類LaravelMongoDB資料庫
- C# 擴充套件方法 借籤於 Objective-C 擴充套件類.C#套件Object
- 擴充套件叢集blk數套件
- 數論分塊擴充套件套件
- es6 陣列擴充套件方法陣列套件
- Java方法04:擴充命令列傳參Java命令列
- dart系列之:dart類的擴充套件Dart套件
- Swift快速為類擴充套件屬性Swift套件
- 五個檢視擴充套件類 LL套件