if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q balss and r R balls ending with Q and R .
if ( last == 0 ) dp [ p , q , r , last ] = countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ;