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;
}
相關文章
- PowerToys外掛擴充套件(類似Alfred)套件Alfred
- Fibonacci數列
- 整數取模類
- 轉向Kotlin——列舉類和擴充套件Kotlin套件
- C#DateTime類擴充套件——獲取旬、季、年天數方法C#套件
- 分類與擴充
- 分類擴充套件套件
- HttpContext擴充套件類HTTPContext套件
- 等比數列求和技巧(公式+倍增)公式
- 藍橋杯:入門訓練 Fibonacci數列
- 數列求和【線段樹基礎】
- JMeter 擴充套件開發:擴充套件 TCP 取樣器JMeter套件TCP
- C#列舉(一)使用總結以及擴充套件類分享C#套件
- [擴充套件推薦] 使用 laravel-gridCaptcha 本地生成類似於谷歌點圖驗證碼套件LaravelAPT谷歌
- 【IDL】 DICOM擴充套件模組套件
- 隔列求和
- Spring Boot中如何擴充套件XML請求和響應的支援Spring Boot套件XML
- 一維陣列:相鄰兩數求和陣列
- es6-陣列擴充套件陣列套件
- weex ios擴充套件類的作用iOS套件
- Java-IoUtil擴充套件工具類Java套件
- C# 反射呼叫擴充類方法C#反射
- Laravel MongoDB 資料庫查詢擴充外掛 擴充原始 Laravel 類LaravelMongoDB資料庫
- DDD福音:Zeebe是一個類似Kafka的可擴充套件的分散式事件溯源工作流引擎Kafka套件分散式事件
- es6 陣列擴充套件方法陣列套件
- Java方法04:擴充命令列傳參Java命令列
- 【演算法】Fibonacci(斐波那契數列)相關問題演算法
- go get 拉取擴充套件報錯Go套件
- 擴充套件叢集blk數套件
- 數論分塊擴充套件套件
- SpringBoot各類擴充套件點詳解Spring Boot套件
- 五個檢視擴充套件類 LL套件
- dart系列之:dart類的擴充套件Dart套件
- JavaScript建立陣列求和JavaScript陣列
- ThinkPHP 類似 AOP 思想的引數驗證PHP
- 【刷演算法】我知道的所有類似斐波那契數列的問題演算法
- Linux 擴充套件磁碟分割槽(命令列操作)Linux套件命令列
- 2020-10-16(陣列方法的擴充)陣列
- ES6之陣列的擴充套件陣列套件