Given n words repeated in a b-beat cycle, find the turn on which a target word is shouted for the X-th time.
Medium6MathBinary searchPrefix sumImplementationNo attempts yetTime limit1sMemory limit128 MBDidi's favorite random game ~! Which game ~! Game start!!
Strawberry carrot watermelon chamoe melon game♪
At Didi University the strawberry carrot watermelon chamoe melon game runs by these rules.
if (b == 1) printf("%s", S[1]);
else {
if ((i - 1) % (2 * (b - 1)) + 1 < b) {
for (int j = 1; j <= (i - 1) % (b - 1) + 1; j++)
printf("%s ", S[(j - 1) % n + 1]);
} else {
for (int j = 1; j <= b - ((i - 1) % (b - 1)); j++)
printf("%s ", S[(j - 1) % n + 1]);
}
}
The word set S is numbered from 1.
With n=3, b=5 and the words king, god, gd in that order, the turns go like this.
The same pattern continues after that.
Didi wonders which turn holds the moment the word k has been shouted X times. Write a program that answers Didi's question.
The first line contains n and b. (1≤n≤2×105, n≤b≤1012)
The second line contains a word k and X. (1≤X≤1012)
The third line contains the n words of the game, separated by spaces. Every word consists of lowercase letters and is at most 10 characters long.
Print the number of the turn in which the word k is shouted for the X-th time. Only inputs that have an answer are given.