PROBLEM LINKS:DIFFICULTY:Easy PREREQUISITES:Adhoc QUICK EXPLANATION:To make sure that we get a number between L and R, we need to retain all the digits starting from the most significant digit which are equal in both L and R as long as we find a difference in the digits of any corresponding position. Next we try to fill the remaining positions using only 4's and 7's. But before that we need to ensure that we keep the number being formed less than R and greater than L. Note that it can be equal to either of them. It's enough if we ensure this condition at a single digit position. For example, L=144239 and R=144880. For this, if we for a number X=1445??, we have already ensured that it is between L and R and hence we can fill the other ?'s using 4's and 7's. Suppose we form the number like X=1442??, then it's still not ensured that L<=X<=R. Hence this has to be taken care in one of the next positions. Similarly X=1448?? is not ensured to be in between. Once you ensure that it lies in between, we can fill all the lesser significant positions using any combination of 4's and 7's. We choose the combination for which the product is maximum. DETAILED EXPLANATION:Tip : In this example we'll treat numbers as an array of digits with the most significant digit having the highest index. For example, if the number is 45382 consider it to be an array like the following:
The problem requires you to find a number between L and R for which F4(x) * F7(x) is maximum. Let us try to find a number between L and R first. To do that, let's make both the numbers of equal length by appending 0's to the smaller number(if required). For example, if you have L=238 and R=1209, append a single '0' to L to make it 0238. Now, both L and R are of length 4. Next, we try to find the first position where both the numbers differ, starting from the most significant digit. Let's call that position 'i'. Obviously, L[i] < R[i]. As we keep checking for the difference in both the numbers, we count the number of 4's (n4) and 7's (n7) encountered so far. If i<0, this means both the numbers are same and our answer is n4*n7. Now we can find a number between L and R in the following way. Suppose the number is X. And let X[y] denote the yth digit of X. Keep every digit in X from most significant position to i+1 same as that of L and R. For example, L = 410327 and R = 410989. Here i=2 becase L[k] = R[k] for k > 2
Let's divide the numbers into 3 categories:
Let's discuss case 1 first. L[i] < X[i] < R[i] ensures that no matter what, X will always be in between L and R. So we try to maximize the product of n4 and n7 by filling the lesser significant digits(i.e. Digits at positions < i ) using only 4's and 7's. We already have n4 4's and n7 7's. So we try out filling all the remaining i positions using s4 4's and s7 7's such that s4 + s7 = i. Also, if X[i] is 4 or 7, add it to corresponding count (either n4 or n7). Now we try out all different combinations for which s4 + s7 = i and look for the one for which (s4+n4) * (s7+n7) is maximum. This can be done easily by looping from j=0 to i1 and considering that there are j 4's and (i  j) 7's in the remaining digits of x. For example, in the above given case, we can have X[i] = 4/5/6/7. And the rest two digits i.e. X[1] and X[0] can be 44,47,74 and 77 so as to maximize the products. Next we come to case 2 i.e. X[i] = L[i]. To get a number between, L and R, we can do the following: X[j] = L[j] for j=i to k. Then, to make X larger than L, we have L[k1] < X[k1]. Now we have the same case as 1 with 'i' modified to k1. We apply the same method and try to maximize the product. But here, we need to do it for every possible 'k' i.e. from i to 0. In the case 3, we have X[i] = R[i]. To get a number between, L and R, we can do the following: X[j] = R[j] for j=i to k. And to have X smaller than R, we have X[k1] < R[k1]. Then proceed as case 1. Again, we would have to try out for all possible k i.e. from i to 0. The maximum out of all the three cases will be our final answer. Let's take a look at the complexity of the solution. Let N be the length of R. Case 1 takes atmost N * B operations where B is the number of digits in base 10 i.e. 10. Case 2 & 3 both have to do the same N*B operations as mentioned above. But these operations have to be repeated for different 'k' which in the worst case will be N. Thus there will be total of NNB operations. Hence, the complexity of the solution is O(T * N * N * B). This solution can be improved in the following manner: In the existing solution, we can observe that given n4, n7 and i, we were calculating the maximum possible product by trying to replace every digit at index less than i with either 4 OR 7. Let's call this value f(n4, n7, i). Instead of looping over for all values between 0 to i, the function can give the same results if modified in the following manner:
This function does the same job in O(1) time rather than O(i) time. To understand this lets consider that there are k 4's and (ik) 7's in the remaining i positions. Then the product F4(x)*F7(x) will be (n4+k) * (n7+(ik)). This is a quadratic equation in k. Hence the only possible points where the value can be maximum is when k=(i+n7n4)/2 or at the end points i.e. K=0 or k=i. Hence this can be calculated in O(1) now. A futher optimization can be to precalculate the values for given n4, n7 and i for every pair of L and R i.e. precalculating f(L,R,n4,n7,i) in the following way:
The function is quite easy to understand. Using the above 2 optimizations, we can get a much efficient solution. SETTER'S SOLUTION:Will be updated soon. APPROACH:The problem setter used the above approach in his solution. TESTER'S SOLUTION:Can be viewed here. APPROACH:The problem setter used the above approach in his solution.
This question is marked "community wiki".
asked 12 Jun '12, 02:06
showing 5 of 9
show all

Very Nice Editorial. The quality of the editorials has definitely improved a lot. answered 14 Jun '12, 20:00

How i=2 . 'i' the index where both numbers differ? answered 15 Jun '12, 13:23

At least you can put some comments in the solution. We don't know what each variable denotes. answered 15 Jun '12, 14:11

Pls tell me what is problem with my submitted code 1106402. I have followed the same approach. And checked atleast 400 cases virtually all icould think of,
paste the direct link to your submission ;)
link to my solution is http://www.codechef.com/viewsolution/1106402 . Pls u can only provide me test cases where it is failing.
Pls admin or any codeshef user who wants to test himself in finding tricky cases and whoever wants to help...pls refer to my above link and suggest me which case my code is failing...
Why are you using this?
printf("\n%d",max_prod);
read more about here  http://www.codechef.com/wiki/faq#How_does_Codechef_test_whether_my_solution_is_correct_or_not (but this is not the only problem)I think that
printf("\n%d",max_prod);
is not the problem since the judge isignore extra white spaces
. It is very easy for this problem to test yourself. Just write a brute force solution and compare its answer with the answer generated by your current solutions for various random tests. I will write more detailed scheme as an answer.Very nicely explained tutorial. Keep up this good work guys !!
@betlista: anton_lunyov is correct as my previous codes have only this type of output syntax and they are accepted...but i will heed to your suggestion and will try more efficient approach...and yes the other problem!
http://www.codechef.com/viewsolution/1101349 i have used the same procedure with the same optimization in my code, yet i got a TLE. i have tested it on ideone with my own generated test cases. various test files with 2000 test cases each. values ranging from 10^18 to 1. and it runs well within 0.13 secs for each test file. may i know which test case is going wrong for my code ?