In a School, Chocolate bars have to be distributed to children waiting in a queue. Each Chocolate bar is rectangular in shape. Consider its side lengths are integer values.
The distribution procedure is as follows:-
If bar is not square in shape, then the largest possible square piece of Chocolate is broken and given to the first child in queue. If bar is square in shape, then complete bar is given to the first child in queue. Once a child receives his share of Chocolate, he leaves the queue. The remaining portion of the Chocolate bar is dealt in same fashion and the whole or a portion of it is given to the next child in the queue.
School has got a carton of Chocolate bars to be distributed among the children all over the School. The Chocolate bars in the carton are of different sizes. A bar of length
jis considered to be different from a bar of length j and breadth
i. For every
isuch that \$M \le i \le N\$ and every
jsuch that \$P\le j \le Q\$ (where M, N, P and Q are integers). Each Chocolate bar in carton is unique in length (
i) and breath(
Given the values of M, N, P and Q (where M, N values are the ranges for length of Chocolate and P, Q values are the ranges for breadth of the chocolate). Find the number of children who will receive Chocolate from the carton.
M, N, P, Q are of integer type (M, N values are the ranges for length of chocolate bar. P, Q values are the ranges for breadth of chocolate bar).
Number of children who will receive Cadbury bar from the carton.
M = 5, N = 6, P = 3, Q=4 Here,
ican be from 5 to 6 and
jcan be from 3 to 4. So the four bars will be in carton of sizes 5x3, 5x4, 6x3, 6x4.
First we choose a Cadbury bar of size 5x3 → first child would receive 3x3 portion ( remaining 2x3 portion ) → next child would receive 2x2 portion ( remaining 2x1 portion ) → now the remaining portion are 2 square pieces of (1x1), which can be given to 2 more children
So the Cadbury bar with the size of 5x3 can be distributed to 4 children.
Similarly we can find out number of children for rest of the combinations (i.e. 5x4, 6x3, 6x4) in the given range as follows
Can anyone suggest improvements?
m, n, p, q = raw_input().split(":") ll = int bb = int count = 0 def cadbury(m, n, p, q): leng = [m, n] brd = [p, q] for l in leng: for b in brd: call(l, b) def call(l, b): #print l,b global count area = l * b if l > b: bb = l rem = bb - b #print rem rem_part1 = rem #print rem_part1 rem_part2 = b #print rem_part2 l = rem_part1 #print l b = rem_part2 #print b if l != b: if (l==1) or (b==1): count += 1 count += (l*b) #print count return else: count += 1 #print count call(l, b) if l==b: count+=2 #print count return elif b > l: ll = b #print ll rem = ll - l #print rem rem_part1 = rem #print rem_part1 rem_part2 = l #print rem_part2 l = rem_part1 #print l b = rem_part2 #print b if l != b: if (l==1) or (b==1): count += 1 count += (l*b) #print count return else: count += 1 #print count call(l, b) if l==b: count+=2 #print count return cadbury(int(m), int(n), int(p), int(q)) print count