Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int result(int N, int R, int S, int E, int inverse)
- {
- const int half = pow(2, N-1);
- // reached the base case
- if (N==0)
- return inverse;
- // if R is in the upper half
- else if (R < half)
- {
- if (E<half) /*if the sum required is in the first quarter*/
- return result(N-1, R, S, E, inverse);
- if (S>=half) /*if the sum required is in the second quarter*/
- return result(N-1, R, S-half, E-half, inverse);
- else /*if the sum required is shared between the first and the second quarter*/
- return ( result(N-1, R, half-S-1, half-1, inverse)+result(N-1, R, half, E, inverse) );
- }
- // if R is in the lower half
- else
- {
- if (E<half) /*if the sum required is in the third quarter*/
- return result(N-1, R-half, S, E, inverse);
- else if (S>=half) /*if the sum required is in the fourth quarter*/
- return result(N-1, R-half, S-half, E-half, inverse*-1);
- else /*if the sum required is shared between the third and the fourth quarter*/
- return ( result(N-1, R-half, half-S-1, half-1, inverse)+result(N-1, R-half, half, E, inverse*-1) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement