i had some how but cant figure out the problemto solve this problem of fibonacci sequence.Fibonnaci numbers commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. what i did im doing im checking range between number after that what to do?
F0 = 0
F1 = 1
Fn = Fn − 1 + Fn − 2, for integer n > 1.
One way of generalizing the Fibonacci sequence is by starting with any pair of numbers and extending to negative values of n.
Given two terms of a generalized Fibonacci sequence a and b, their positions c and d respectively and a position e, find res.
input
0 1
6 13
10
output
89
input
3 65
6 315
-10
output
4620
input
9 36
15 646
-5
output
-1/4
void solve(int N, int K)
{
vector<long long int> Array(N + 1, 0);
// If N is less than K
// then the element is '1'
if (N <= K) {
cout << "1" << endl;
return;
}
long long int i = 0, sum = K;
// first k elements are 1
for (i = 1; i <= K; ++i) {
Array[i] = 1;
}
// (K+1)th element is K
Array[i] = sum;
// find the elements of the
// K-Fibonacci series
for (int i = K + 2; i <= N; ++i) {
// subtract the element at index i-k-1
// and add the element at index i-i
// from the sum (sum contains the sum
// of previous 'K' elements )
Array[i] = sum - Array[i - K - 1] + Array[i - 1];
// set the new sum
sum = Array[i];
}
cout << Array[N] << endl;
}
// Driver code
int main()
{
long long int N = 4, K = 2;
// get the Nth value
// of K-Fibonacci series
solve(N, K);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire