Skip to content

Commit df361c8

Browse files
committed
Fibosum from SPOJ
1 parent 3096dad commit df361c8

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

FIBOSUM-h.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
#define mod 1000000007
5+
int size=2;
6+
void multiply(long long int first[][3],long long int second[][3])
7+
{
8+
long long int answer[10][10]={};
9+
for(int i=0;i<size;i++){
10+
for(int j=0;j<size;j++){
11+
for(int k=0;k<size;k++){
12+
answer[i][j]=answer[i][j] + (first[i][k]%mod * second[k][j]%mod)%mod;
13+
answer[i][j]%=mod;
14+
}
15+
}
16+
}
17+
for(int i=0;i<size;i++){
18+
for(int j=0;j<size;j++){
19+
first[i][j]=answer[i][j];
20+
}
21+
}
22+
}
23+
24+
void power(long long int second[][3],long long int n){
25+
long long int current[3][3]={},i,j;
26+
for(i=0;i<size;i++){
27+
for(j=0;j<size;j++)
28+
current[i][j]=second[i][j];
29+
}
30+
if(n<=1){
31+
return;
32+
}
33+
if(n&1){
34+
power(second,n-1);
35+
multiply(second ,current);
36+
}
37+
else{
38+
power(second,n/2);
39+
multiply(second,second);
40+
}
41+
}
42+
int main(){
43+
int t;
44+
cin>>t;
45+
while(t--){
46+
long long int a,b;
47+
cin>>a>>b;
48+
long long int myArray[3][3]={},answera=0,answerb=0;
49+
if(a==0)
50+
answera=0;
51+
else if(a==1)
52+
answera=0;
53+
else if(a==2)
54+
answera=1;
55+
else if(a==3)
56+
answera=2;
57+
else{
58+
myArray[0][0]=1;myArray[0][1]=1;myArray[1][0]=1;myArray[1][1]=0;
59+
power(myArray,a);
60+
answera=(myArray[0][0]-1)%mod;
61+
}
62+
63+
if(b<=2){
64+
answerb=b;
65+
}
66+
else{
67+
myArray[0][0]=1;myArray[0][1]=1;myArray[1][0]=1;myArray[1][1]=0;
68+
power(myArray,b+1);
69+
answerb=(myArray[0][0]-1)%mod;
70+
71+
}
72+
73+
cout<<(answerb-answera+mod)%mod<<"\n";
74+
}
75+
}

0 commit comments

Comments
 (0)