<p>You are given a string <code>s</code> of <strong>even length</strong> consisting of digits from <code>0</code> to <code>9</code>, and two integers <code>a</code> and <code>b</code>.</p>

<p>You can apply either of the following two operations any number of times and in any order on <code>s</code>:</p>

<ul>
	<li>Add <code>a</code> to all odd indices of <code>s</code> <strong>(0-indexed)</strong>. Digits post <code>9</code> are cycled back to <code>0</code>. For example, if <code>s = &quot;3456&quot;</code> and <code>a = 5</code>, <code>s</code> becomes <code>&quot;3951&quot;</code>.</li>
	<li>Rotate <code>s</code> to the right by <code>b</code> positions. For example, if <code>s = &quot;3456&quot;</code> and <code>b = 1</code>, <code>s</code> becomes <code>&quot;6345&quot;</code>.</li>
</ul>

<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain by applying the above operations any number of times on</em> <code>s</code>.</p>

<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears earlier in the alphabet than the corresponding letter in <code>b</code>. For example, <code>&quot;0158&quot;</code> is lexicographically smaller than <code>&quot;0190&quot;</code> because the first position they differ is at the third letter, and <code>&#39;5&#39;</code> comes before <code>&#39;9&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;5525&quot;, a = 9, b = 2
<strong>Output:</strong> &quot;2050&quot;
<strong>Explanation:</strong> We can apply the following operations:
Start:  &quot;5525&quot;
Rotate: &quot;2555&quot;
Add:    &quot;2454&quot;
Add:    &quot;2353&quot;
Rotate: &quot;5323&quot;
Add:    &quot;5222&quot;
Add:    &quot;5121&quot;
Rotate: &quot;2151&quot;
Add:    &quot;2050&quot;​​​​​
There is no way to obtain a string that is lexicographically smaller than &quot;2050&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;74&quot;, a = 5, b = 1
<strong>Output:</strong> &quot;24&quot;
<strong>Explanation:</strong> We can apply the following operations:
Start:  &quot;74&quot;
Rotate: &quot;47&quot;
​​​​​​​Add:    &quot;42&quot;
​​​​​​​Rotate: &quot;24&quot;​​​​​​​​​​​​
There is no way to obtain a string that is lexicographically smaller than &quot;24&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;0011&quot;, a = 4, b = 2
<strong>Output:</strong> &quot;0011&quot;
<strong>Explanation:</strong> There are no sequence of operations that will give us a lexicographically smaller string than &quot;0011&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= s.length &lt;= 100</code></li>
	<li><code>s.length</code> is even.</li>
	<li><code>s</code> consists of digits from <code>0</code> to <code>9</code> only.</li>
	<li><code>1 &lt;= a &lt;= 9</code></li>
	<li><code>1 &lt;= b &lt;= s.length - 1</code></li>
</ul>

class Solution:
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
        # An O(N log N) solution
        n, t = len(s), s+s
        # First, we find all candidates [(idx, k1, k2), ...] for the starting index of our string 
        # such that all even positions are optimal by adding g a total of k1 times and all odd positions 
        # by adding g a total of k2 times.
        g = gcd(a, 10)
        candidates = [(i, int(t[i])//g if b & 1 else 0, int(t[i+1])//g) for i in range(0, n, gcd(b, n))]
        # Store all the prefix hashes
        M = 10**9+7
        even = [list(accumulate([(int(t[i]) - j*g)%10 * pow(10, i, M) if not i & 1 else 0 for i in range(2*n)], initial=0)) for j in range(0, 10//g)]
        odd = [list(accumulate([(int(t[i]) - j*g)%10 * pow(10, i, M) if i & 1 else 0 for i in range(2*n)], initial=0)) for j in range(0, 10//g)]
        def are_equal(c1, c2, length):
            # Check if t[c1[0]:c1[0]+length] altered by c1[1:3] == t[c2[0]:c2[0]+length] altered by c2[1:3]
            # It is given that c1[0] < c2[0]
            c1even, c1odd = c1[1 + (c1[0]&1)], c1[1 + (not c1[0]&1)]
            c2even, c2odd = c2[1 + (c2[0]&1)], c2[1 + (not c2[0]&1)]
            lhs = even[c1even][c1[0]+length] - even[c1even][c1[0]] + odd[c1odd][c1[0]+length] - odd[c1odd][c1[0]]
            rhs = even[c2even][c2[0]+length] - even[c2even][c2[0]] + odd[c2odd][c2[0]+length] - odd[c2odd][c2[0]]
            return lhs * pow(10, c2[0] - c1[0], M) % M == rhs % M

        def convert(cand, r):
            return (int(t[cand[0]+r]) - (cand[2]*g if r & 1 else cand[1]*g)) % 10

        # We now select the best candidate
        best = candidates[0]
        for cand in candidates[1:]:
            # Check if best is better than cand.
            # I.e., check if t[best[0]:best[0]+n] altered by best[1:3] <= t[cand[0]:cand[0]+n] altered by cand[1:3]
            l, r = 0, n+1
            while r > l:
                mid = (l+r) >> 1
                if are_equal(best, cand, mid): l = mid + 1
                else: r = mid
            r -= 1
            if convert(best, r) > convert(cand, r):
                best = cand
                
        return ''.join(str(convert(best, i)) for i in range(n))
17000