Algorithm
Problem link- https://www.spoj.com/problems/TETRAHRD/
TETRAHRD - Sum of Tetranacci numbers
The sequence of Tetranacci numbers is defined as follows:
an = an-1 + an-2 + an-3 + an-4 with a0 = a1 = a2 = 0 and a3 = 1.
Input
Input starts with a positive integer t ≤ 4000, then t lines follow. Each of the t lines contains two space separated integers m and n with 0 ≤ m ≤ n ≤ 109.
Output
Calculate am + am+1 + ... + an and print the result modulo 1000000007.
Example
Input: 2 1 2 1919 5393 Output: 0 66616
Note: If your solution times out, you may try the tutorial version first.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007LL
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI 3.14159265358979323846
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
const int MAXN = 1e5+5;
ll add(ll a, ll b){
ll res = a + b;
if(res >= MOD)
return res - MOD;
return res;
}
ll mult(ll a, ll b){
ll res = a * b;
if(res >= MOD)
return res % MOD;
return res;
}
const int SZ = 5;
struct matrix{
ll arr[SZ][SZ];
void reset(){
memset(arr, 0, sizeof arr);
}
void makeiden(){
reset();
for(int i = 0;i < SZ; i++)
arr[i][i] = 1;
}
matrix operator + (const matrix &o) const{
matrix res;
res.reset();
for(int i = 0;i < SZ; i++)
for(int j = 0;j < SZ; j++)
res.arr[i][j] = add(arr[i][j], o.arr[i][j]);
return res;
}
matrix operator * (const matrix &o) const {
matrix res;
res.reset();
for(int i = 0;i < SZ; i++)
for(int j = 0;j < SZ; j++)
for(int k = 0;k < SZ; k++)
res.arr[i][j] = add(res.arr[i][j], mult(arr[i][k], o.arr[k][j]));
return res;
}
};
matrix power(matrix a, int b){
matrix res;
res.makeiden();
while(b){
if(b & 1){
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
matrix base;
matrix init;
int main(){
io;
base.reset();
init.reset();
base.arr[0][0] = 1;
base.arr[0][1] = 1;
base.arr[1][1] = 1;
base.arr[1][2] = 1;
base.arr[1][3] = 1;
base.arr[1][4] = 1;
base.arr[2][1] = 1;
base.arr[3][2] = 1;
base.arr[4][3] = 1;
init.arr[1][0] = 1;
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m;
ll sumN, sumM;
if(n == 0 || n == 1 || n == 2)
sumN = 0;
else if(n >= 3)
sumN = (power(base, n-3) * init).arr[0][0];
if(m == 0 || m == 1 || m == 2)
sumM = 0;
else
sumM = (power(base, m-2) * init).arr[0][0];
ll output = ((sumM-sumN) + MOD) % MOD;
cout << output << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 2
1919 5393
Output
66616
Demonstration
SPOJ Solution-Sum of Tetranacci numbers-Solution in C, C++, Java, Python