#P7350. 「MCOI-04」Dream and Strings

「MCOI-04」Dream and Strings

Description

Tommy’s encryption system uses the following string hash algorithm:

int Hash(std::string s, int base, int mod) {
  int result = 0;
  for(int i=0; i<s.length(); i++)
    result = (1ll * result * base + s[i]) % mod;
  return result;
}

Here, base\texttt{base} and mod\texttt{mod} are given parameters, satisfying 257basemod109+9257\le\texttt{base}\le\texttt{mod}\le10^9+9, and mod\texttt{mod} is a prime number.

Now Dream wants to crack Tommy’s ciphertext. First, Dream needs to find a suitable hash collision. Given base\texttt{base}, mod\texttt{mod}, and a string SS, find a string TT of the same length such that S=T|S|=|T|, hash(S)=hash(T)\texttt{hash(S)=hash(T)}, but SS and TT differ at least at one position.

If there are multiple valid TT, output any one of them.

Input Format

The first line contains two positive integers base\texttt{base} and mod\texttt{mod}.
The second line contains a string SS consisting of lowercase letters.

Output Format

Output one line containing a string TT consisting of lowercase letters, representing the answer.

257 997
aabdccbabdcdcadbcabcabaabdbbddbaabcadabcbcdacbbaac
lmzaeccihyailccmzzaxshssgbvjvhthllyofzudraatifnzxy

Hint

Constraints

If SS is a string, S|S| is its length.

For the first 10%10\% of the testdata, mod=997\texttt{mod}=997.
For the first 40%40\% of the testdata, S=2×105|S|=2\times10^5 and SS is random.
For 100%100\% of the testdata, 50S2×10550\le|S|\le2\times10^5, 257base<mod109+9257\le\texttt{base}<\texttt{mod}\le10^9+9, and mod\texttt{mod} is a prime number.

Notes

Minecraft OI Round 4 D
idea & solution: w33z8kqrqk8zzzx33 check: MatKave

Translated by ChatGPT 5