Monday, 20 February 2017

Equation

QUESTION DESCRIPTION :

Given N,A,B,C, find how many solutions exist to the equation : a + b + c <= N, such that 0 <=a <=A, 0 <= b <= B, 0<= c <= C.

Input

The first line contains the number of test cases T. Each test case contains 4 integers, N,A,B,C. 0 <= N,A,B,C <= 2500

Output

Output T lines, one for each test case.


TEST CASE 1

INPUT

2
4 3 2 1
1 1 1 1 

OUTPUT

20
4


EXPLANATION :

The program requires you to take 5 integers as input from the user . The first number is the number of time the user wants you to run your code ( number of test cases ). The second number is N , the third one is A , fourth one is B , fifth one is C.
The algorithm used is brute forcing ( Brute Forcing, using a quadruple nested loop ( Nested Loops ) , with 3 for loops used for the core algorithm.

To do the question we take all values of a,b,c which satisfy a<=A , b<=B , c<=C and check weather they satisfy a+b+c<=N , if that is true we increase the value of the counter by 1 ( i ) . Then finally after testing all cases the result is printed. Then before going to the next test case , we decrease number of test cases ( t--  ) by 1 and reset counter to 0 ( i=0 )


ALGORITHM :
  • Take an integer input which signifies the number of test cases ( t )
  • Run a loop from 0 till < t or from t till t > 0
  • Input the first test case values ( N,A,B,C )
  • Run 3 nested loops , first till A , second till B , third till C
  • In the third loop test for the valid case ( a+b+c<=N )
  • If the case is true , increase counter by 1 ( i++ )
  • After the loops end print the counter ( printf("%d",i); )
  • Reset the counter to 0 and decrease the test case by 1 ( i=0 , t-- )
  • Rerun the loops again for the new values , till t is 0
  • Return and exit


SOLUTION : (Please use this as reference only)


CODE

No comments:

Post a Comment