PROBLEM LINKSDIFFICULTYEASY PREREQUISITESBrute force, Greedy PROBLEMWe are given an integer S represented in a 7segment display. Add some segments or digits into it to create the as large number as possible not larger than M. QUICK EXPLANATIONSince we want to maximize the number, it is always optimal to make S contain the same number of digits as M (assume that we allow leading zeros). Hence, there are two steps in the solution:
We can perform all possible ways of the first step using brute force. For each intermediate result after the first step, the second step can be performed using greedy. Finally, we output the largest resulting number over all ways. EXPLANATIONFirst, let's introduce a compatibility matrix valid[i][j] = whether digit i can be transformed into digit j by adding some segments. The value of each cell of the matrix can be computed by hand. Here are the values: \ 0123456789 + 01000000010 11101100111 20010000010 30001000011 40000100011 50000011011 60000001010 71001000111 80000000010 90000000011 Let's go through the steps in more details. Step 1Let X be the number of digits of X. As mentioned before, in an optimal solution the number of digits of M and S must be equal. There are exactly M  S + 1 possible ways to add new digits. For example, suppose S = 89375 and M = 9247529. Then, there are 3 ways:
Here, the new digits are represented by X's for convenience. Let's call the new integer after adding some digits as S*. Step 2Note that the restriction that the resulting number must not have leading zeros is so that we cannot have the length resulting number is greater than M but that difference of length consists of all leading zeros. If that is allowed, then for test case like S = 0, M = 9 we can have 09 as the resulting number, hence making the problem trickier. Since we have in our solution that S = M, we can safely ignore that restriction. For each possible intermediate result of Step 1, we must replace each X by an actual digit and optionally add some segments to the existing digits. We can use a greedy method here. We will iterate S* from the most significant digit through the least significant digit. For each digit, we try to transform it into the largest digit, while maintaining that the final result is not greater than M. This way, it can be proved that the result is the largest possible. When iterating the digits from the most significant digit through the least significant digit, the choice of the current digit depends on the current prefixes of M and S*. Suppose we are now considering the ith most significant digit. The current state would be like this: M = Pp... S* = Qq... ^ ith most significant digit where p and q are M anp S*'s ith most significant digits, respectively, while P and Q are M and S*'s current prefixes, respectively. The (i+1)th through the Mth digits are not considered yet. For example, let M = 9247529, the currently built S* = 89375XX, and i = 4, then the current state is: M = 9247... S* = 8937... ^ 4th most significant digit where P = 924, Q = 893, p = 7, and q = 7. For each state, i.e. for each step in the iteration, there are 2 possibilities to maintain that the resulting number is not greater than M: q > p. Here, Q must be strictly less than P because otherwise the resulting number would be greater than M. q ≤ p. Here, Q must be less than or equal to P. From the above explanation, let's maintain two values while iterating the digits:
We update the two values after each step in the iteration. In the end, we choose the iteration that yields the maximum value. Please consult the following pseudocode for more details. The time complexity of this solution is O(M^2). // returns the maximum possible resulting number // if we align the first digit of S with the kth digit of M // or 1 if it is impossible. function solve(M, S, k): prefix_less = 1 prefix_equal = 0 for i = 1; i ≤ M; i++: // if the resulting number must be greater than M, then impossible if prefix_less == 1 and prefix_equal = 1 return 1 // update prefix_less for d = 9; d ≥ 0; d: // if we cannot transform the digit, continue. if i ≥ k and i ≤ k + S  1 and not valid[S[i  k + 1]][d]: continue // found the largest valid digit. // if the new digit is greater than the current digit, use prefix_less if d > M[i]: if prefix_less != 1: prefix_less = prefix_less * 10 + d // if the new digit is not greater than the current digit, use either prefix_less or prefix_equal else if prefix_less != 1: if max(prefix_less, prefix_equal) != 1: prefix_less = max(prefix_less, prefix_equal) * 10 + d break // update prefix_equal if i ≥ k and i ≤ k + S  1 and not valid[S*[i  k + 1]][M[i]]: prefix_equal = 1 else prefix_equal = prefix_equal * 10 + M[i] // return the better answer return max(prefix_less, prefix_equal) // the main code read(S) read(M) best = 0 for k = 1; k ≤ M  S + 1; k++: best = max(best, solve(M, S, k)) print(best) SETTER'S SOLUTIONCan be found here. TESTER'S SOLUTIONCan be found here.
This question is marked "community wiki".
asked 21 Jan '13, 00:29

Correction:
should be
answered 21 Jan '13, 02:27

Correction#2:
should be
answered 21 Jan '13, 02:31

can you please tell me where my code is failing, i used the same approach as described in the editorial answered 21 Jan '13, 08:28
You print nothing for this test: 0 6
(21 Jan '13, 13:34)
you can check the link. My program prints 0 for this test case. and it is giving correct answers for all the test cases posted below
(21 Jan '13, 15:17)
Yes, your bug is quite tricky. It is a big luck that our test data was managed to cover it. Try this test: Just to make sure that this time I am correct I ideoned your code against this test and it indeed prints nothing for 0 6 now.
(21 Jan '13, 22:50)
I just want to kick myself. had the fastest solution but could not find the mistake after spending 2 hours on it during the contest
(22 Jan '13, 10:10)

It would be nice if you could post the input set used to validate the solutions... answered 21 Jan '13, 08:55
Refer to this:
(21 Jan '13, 13:59)

Some of the tricky test cases: 9 274 4883530 5 268343 2 558870 10381 16146 0 6 0 9 2 200543 4987565 14398964 1042216 1815366with the corresponding answers 4883499 268343 558870 10989 0 8 200543 9989989 1098898Will be updated regularly ;) answered 21 Jan '13, 13:58

Let's add another constraint to make it harder: the chef can perform the defacing operation at most K times. Share your solution. answered 21 Jan '13, 21:39

They say this is an easy problem. It took me 2 days to understand Anton's code snippet
WTH! :( From where do you come up with these tricks, Anton? I finish the 2.5 hours time only thinking how to solve a problem...God knows when the hell am I gonna actually write more than one solutions in a CookOff or a Long Contest as well. God bless me! answered 23 Jan '13, 00:52
Hm... I thought it should be clear.
(23 Jan '13, 01:09)
@anton_lunyov >> I got what you have written, but I took too much time to understand :( Thats why I was in the phase of sadness as to when will I be able to write such a code :D
(23 Jan '13, 02:42)

My code is resulting in wrong answer ..... http://www.codechef.com/viewsolution/1775055 I have tried every test case given in posts and problem.... working correctly... what is that I am missing ? answered 02 Feb '13, 13:30
Your program fails at the very first test from here. Did you write this just for fun: "I have tried every test case given in posts and problem...." ?
(02 Feb '13, 16:50)
words are encouraging !!!! Still I stand to my words... "it satisfies the cases that you gave...." (not fun this time !!!) run it with gcc compiler... and see
(02 Feb '13, 20:01)
is there a problem with "memset()" ... because locally it is working fine !!!
(02 Feb '13, 20:04)
Haha. Very funny. You've fixed the bug and changed the submission ID to that passing posted test cases. Now the failing test is I've also added it to the above post with tricky test cases.
(02 Feb '13, 20:47)

what is with initial values of prefix_equal and prefix_less?? answered 03 Feb '13, 14:20

http://ideone.com/VRvkWc this is my code for the problem.It is returning correct solution for every test case(mentioned). Someone please tell me what is wrong with this because every time I submit it shows wrong answer The only correct solution posted by me is copied. answered 03 Feb '13, 23:46
1
7347946 12524098 > 10999998
(04 Feb '13, 03:09)
I think this is correct. If wrong what is correct solution??
(04 Feb '13, 12:15)
1
You last submission return different answer. You should indicate the submission ID at codechef instead of pasting the code to outside resources.
(04 Feb '13, 13:02)
http://www.codechef.com/viewsolution/1790134 This code is producing right result on my machine(for the above mentioned case as well). Sorry to sound cynical but it is truth
(04 Feb '13, 22:48)
1
19 hours the last submission was 1784916 and it produces wrong answer.
(04 Feb '13, 23:06)

I have written my code for this problem. Can you please tell me whats the format of the program. I mean it is not specified how the input will be taken and how the output should. My code read the file which is in the folder of compiler and gives output in a file. But tester going to know this. He have to change the name of the file according to his testing computer. So please help me out. How the submission is done. Thanks in advance. answered 26 Feb '13, 00:43

@anton_lunyov Excellent problem..my first attempt at codechef and I am enjoying every bit of it. :) This is the latest version of solution I have developed: http://www.codechef.com/viewsolution/1864293 It gives a correct output for all the 12 test cases given in problem, 9 tricky cases added by you plus two more given in other comments. I know, it doesnt mean it is a correct solution but I am unable to understand why I am still getting wrong answer. I have used my own logic, havent looked at your solution yet, I want to figure out the entire solution on my own. Can you can provide some more test cases, which will help me in figuring out the bug on my own. I tried to create test cases on my own, by using random number generator and all, but got correct answers for all of them. So, if you have some more test cases, it will be great P.S. : I dont expect you to go through my code as it can be tricky to understand the logic :P answered 26 Feb '13, 16:47
1
Your WA is actually RE. It seems that for this test Error:1 Error:java.lang.ArrayIndexOutOfBoundsException: 1 Error:java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1
(26 Feb '13, 21:43)
Ohhh You are right, thanks for the input, I'll work on the bug
(27 Feb '13, 18:09)

@syker >> Try these tests: 8 13 8 24 Correct Answer: 8 and 18 respectively Your answer: 89 and 89 UPD. Your code gives correct answer for the above tests when they are performed initially, but gives 89 when they are written after some tests like 10 100. That might be because of faulty/misplaced initialization of some check arrays. answered 28 Feb '13, 12:46

@anton_lunyov I am now struggling with TLE issue for this problem. What I find odd is: time limit is the same irrespective of platform. Isn't some languages faster as compared to others? And I haven't seen a single successful submission for JAVA for this problem. I know, it could be a mere coincidence but it still makes me wonder. I have tried many optimization techniques but none of them are giving any improvements. On my system I see lot of improvements with every major optimization(I have simulated huge inputs, as per given constraints, using random number generators)but when I submit here, I don't see any improvement at all in time, every time i just get 4.04 seconds, not even slightest improvement. Moreover, some optimization techniques which definitely should have worked have actually deteriorated the performance, e.g. if you compare the submissions: http://www.codechef.com/viewsolution/1877893 and http://www.codechef.com/viewsolution/1883458 the only change is in the function convertToNumber(), where instead of using String object, to append digits one by one, I used StringBuilder and append() function, which according to me should give better performance, but instead the time deteriorated to 10 seconds from 4 seconds. Any pointers in this regard? answered 03 Mar '13, 12:53
Actually there were several AC in Java even during live contest:
(03 Mar '13, 23:18)

Why is the answer for 25, 100 is 89 but not 98 ? 025 > 089 > 89 250 > 098 > 98 Somebody please clarify, I'm struggling..... answered 05 Nov '13, 00:26

I posted my code on Ideone: http://ideone.com/tJKTyy I am getting a WA. I think it is passing the test cases given in the question and the comments here (as well as some ones I made myself), but I might simply be misreading. Can someone help me find my problem? Or at least a test case where my program is returning the wrong answer? answered 04 Mar '14, 00:24

For a line like this
why the answer "10089998" is wrong? Your test give me "10099989", but the last "6" cannot become "9".we add the empty slot after 707496:
Then we can get 10099989.