I’ve recently joined TopCoder and have been practicing in the Practice Rooms for the last few days. I came across this problem which I cant seem to solve. Any help will be appreciated.
The problem
The product value of a string is the
product of all the digits (‘0’-‘9’) in
the string. For example, the product
value of “123” is 1 * 2 * 3 = 6. A
string is called colorful if it
contains only digits and the product
value of each of its nonempty
contiguous substrings is distinct.For example, the string “263” has six
substrings: “2”, “6”, “3”, “26”, “63”
and “263”. The product values of these
substrings are: 2, 6, 3, 2 * 6 = 12, 6
* 3 = 18 and 2 * 6 * 3 = 36, respectively. Since all six product
values are distinct, “263” is
colorful.On the other hand, “236” is not
colorful because two of its
substrings, “6” and “23”, have the
same product value (6 = 2 * 3).Return the k-th (1-based)
lexicographically smallest colorful
string of length n. If there are less
than k colorful strings of length n,
return an empty string instead.
My approach
We can’t have ‘0’ and ‘1’ as digits in n.
All digits must be distinct. So to begin with, n should be smaller than 9. (only the digits 2, 3, 4, 5, 6, 7, 8, 9 can be used, each of them only once).
Since we know that, we can start with “23” (the smallest 2-digit colorful string) as the base string and add one of the allowed digits (check if the string is still colorful or not, on each addition) until we reach length n.
Once we reach length n, we can “play around” with the digits to find the k-smallest.
My question
I feel like this approach will not be fast enough. Even if it is, in what systematic way should I play around with the digits, so that I start from the smallest and make my way through the kth-smallest?
How can I make progress on this problem with this approach? Or are there smarter ways to follow in these kind of problems?
I’m not asking for any solutions or anything. I’m just asking for some clues and some lead.
Some problems I solve in seconds, some take hours of thinking and some like this I can’t do it. But I believe all it takes is some practice, but I cannot make progress without someone leading the way.
Thanks in advance =)
*by the way, this question is from SRM 464 DIV 2 – 500pt. problem. All copyright goes to TopCoder.
Topcoder has a forum in which they create a thread for each SRM (464 is here). Maybe your question is already answered there 🙂