All problems

Problem 846

Decoded String at Index

Medium · 20 pts · String, Stack, Math

Problem

An encoded string of lowercase letters and digits 2 to 9 is decoded by reading it from left to right: a letter is appended to the tape, and a digit d makes the whole tape so far repeat d times in total. The decoded tape can be astronomically long. Print its K-th letter (counting from 1). First compute the length after each character; then walk backwards, replacing K by K mod (length before the repetition) at every digit, until K lands on a letter. Some values computed along the way can be larger than a 32-bit int can hold (about 2.1 × 10^9), even when the input looks small, so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).

Input

The first line contains the encoded string S (it starts with a letter). The second line contains K.

Output

Print the K-th letter of the decoded string.

Constraints

2 <= |S| <= 100; 1 <= K <= 10^9; K is at most the decoded length

Example input

leet2code3
10

Example output

o

Explanation

leet2code3 decodes to leetleetcode repeated 3 times: leetleetcodeleetleetcodeleetleetcode. Its 10th letter is o.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Orderly Queue Next: Count Number of Homogenous Substrings