A function is defined as
f(n+2) = 2*f(n+1) - f(n) + 2 , if n is even
f(n+2) = 3*f(n), if n is odd
f(1) = 1 ,f(2) = 1
n > 0
Help me code this function by recursion or dp or matrix exponential?
And can we solve without using recursion or dp??
if n can be equal upto 10 power 5
int func(int n) {
if ( n == 0 ) return 1;
if ( n == 1 ) return 1;
if .... // make your own tests here
return func(n-1); // make your own expressions here
}
First subtract 2 from your expressions to get f(n) on the left hand side.
IF that is your correct recursion relation then you can WRITE DOWN THE EXACT ANSWER EXPLICITLY; you don't actually need to use any of the methods you suggest.
However,
(1) You have changed your post several times, so I'm holding fire.
(2) If n is 100000 then the answer is
(350000+1)/2
and you aren't going to be able to store that in anything except a (VERY) big integer. (EDIT: revised value later, after the OP changed the question).
I don't think you have told us the whole problem. Is there a modulo operation involved?
Just in case you change it again, this is your current problem:
A function is defined as
f(n+2) = 2*f(n+1) - f(n) + 2 , if n is even
f(n+2) = 3*f(n), if n is odd
f(0) = 1 ,f(1) = 1
n > 0
and here are the f(n) values for smallish n to check the exact result against that obtained by recursion:
A function is defined as
f(n+2) = 2*f(n+1) - f(n) + 2 , if n is even
f(n+2) = 3*f(n), if n is odd
f(1) = 1 ,f(2) = 1
You have to answer multiple queries. In each query, you are given two integers L and R and you are required to determine the value of
(f(L) + f(L+1) + ... + f(R-1) + f(R))\ modulo\ P
You have to answer multiple queries. In each query, you are given two integers L and R and you are required to determine the value of
(f(L) + f(L+1) + ... + f(R-1) + f(R))\ modulo\ P
and P = 1000000007
Input Format
First line: Two integers T and, P where T represents the number of queries
Each of the next T lines: Two integers L and R.
L and R<= 10 POWER 18
p <= 10 power 9
f(1) =1 and f(2) =1 , hen how can i do it for large numbers?
Well, in your previous statement (which wasn't the whole problem, was it?) you had
f(0) =1 and f(1) =1
So the whole set of values I got were then wrong ..ggrrr!
Anyway, try again:
work out the EXACT solution for f(n) (easy for odd numbers; requires a bit of double thinking for even numbers - that's a hint!).
Then work out the EXACT solution for the sum f(L) + ... + f(R) (sums of geometric progressions will help).
The modulo operation will only be involved in computing large powers of 3.
In future:
(1) write down the question accurately;
(2) write down the whole question.
Here are your re-computed f(n) values for small n:
although i tried in java but logic remains same
import java.util.*;
public class Main
{
static boolean is_even(long q){
if(q%2==0){
return true;
}
else{
return false;
}
}
You have to use % during the iterations themselves, not just on the final result. In other words, you have to plan your logic such that an overflow can't happen in the first place.
<Edit: I did not report you, but no I'm not giving help through PM. I don't even like these challenges. It's up to you to figure out how to correctly iterate without overflowing.>