Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. int result(int N, int R, int S, int E, int inverse)
  2. {
  3.     const int half = pow(2, N-1);
  4.  
  5.     // reached the base case
  6.     if (N==0)
  7.         return inverse;
  8.  
  9.     // if R is in the upper half
  10.     else if (R < half)
  11.     {
  12.         if (E<half)         /*if the sum required is in the first quarter*/
  13.             return result(N-1, R, S, E, inverse);
  14.         if (S>=half)        /*if the sum required is in the second quarter*/
  15.             return result(N-1, R, S-half, E-half, inverse);
  16.         else                /*if the sum required is shared between the first and the second quarter*/
  17.             return ( result(N-1, R, half-S-1, half-1, inverse)+result(N-1, R, half, E, inverse) );
  18.     }
  19.  
  20.     // if R is in the lower half
  21.     else
  22.     {
  23.         if (E<half)         /*if the sum required is in the third quarter*/
  24.             return result(N-1, R-half, S, E, inverse);
  25.         else if (S>=half)           /*if the sum required is in the fourth quarter*/
  26.             return result(N-1, R-half, S-half, E-half, inverse*-1);
  27.         else                /*if the sum required is shared between the third and the fourth quarter*/
  28.             return ( result(N-1, R-half, half-S-1, half-1, inverse)+result(N-1, R-half, half, E, inverse*-1) );
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement