You ask if regex exists
(xxx | xxxx | xxxxx) *
matches xx ... x, where x occurs 456 times.
Here the solution is in O (n + a ^ 2), where a is the smallest of the numbers on the left (in this case 3).
Suppose your numbers are 6,7,15. I will call the number, expressed as 6x + 7y + 15z "available." You should check if this number is available.
If you can get some number n, then, of course, you can get n + 6, n + 12, n + 18 - in the general case, n + 6k for all k> = 0. On the other hand, if you cannot get some the number n, then n-6 will not be available either (if you can get (n-6), then (n-6) + 6 = n will be available), which means that n -12, n-18, n- 6k is also not available.
Suppose you have determined that 15 is available, but 9 is not. In our case, 15 = 6 * 0 + 7 * 0 + 15 * 1, but it canβt get 9. So, according to our previous considerations, 15 + 6k is available for all k> = 0 and 9-6k for all k> = 0. If you have some number divided by 6, you get 3 as the remainder (3, 9, 15, 21, ...), you can quickly answer: digits <= 9 are not available, digits> = 15.
It is enough to determine for all possible residuals of dividing by 6 (i.e., 0,1,2,3,4,5), which is the smallest number that is available. (I just showed that this number for remainder 3 is 15).
How to do it: Create a graph with vertices 0,1,2,3,4,5. For all the k numbers given to you (7.15 - ignore 6), add an edge from x to (x + k) mod 6. Give it the weight (x + k) div 6. Use Dijkstra's algorithm , using 0 as the starting node. The distances found by the algorithm will be exactly the numbers we are looking for.
In our case (6,7,15), the number 7 leads to 0 β 1 (weight 1), 1 β 2 (weight 1), 2 β 3 (weight 1), ..., 5 β 0 (weight 1), and the number 15 gives 0 β 3 (weight 2), 1 β 4 (weight 2), ..., 5 β 1 (weight 2). The shortest path from 0 to 3 has one edge - its weight is 2. Thus, 6 * 2 + 3 = 15 is the smallest number that gives 3 as the remainder. 6 * 1 + 3 = 9 is not available (well, we checked it earlier manually).
And what is the relationship with regular expressions? Well, every regex has an equivalent finite state machine, and I built one of them.
This problem, resolved by several requests, appeared at the Polish Olympiad, and I translated the solution. Now, if you hear now that a person speaking in computer science is not useful for real programmers, hit him in the face.