Problem:
﻿Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

S is a string of length between 1 and 100 (inclusive).

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]

use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, Write};
use std::ops::{
    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
    Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
};
use std::str::{self, FromStr};
use std::{char, cmp, f32, f64, fmt, i16, i32, i64, i8, thread, u16, u32, u64, u8};

fn main() {
    scan_all(io::stdin(), 1024);
    let (s, k) = (r::<String>(), r::<u64>());
    let (s, n) = (s.as_bytes(), s.len());
    if s.iter().all(|&c| c == b'1') {
        return println!("1");
    }
    let i = (0..n).filter(|&i| s[i] > b'1').next().unwrap();
    let r = if k - 1 < (i as u64) { b'1' } else { s[i] };
    println!("{}", r as char);
}

thread_local! {
    static SCANNED: RefCell<Scanned> = RefCell::default();
}

fn scan_all<R: Read>(mut input: R, estimated: usize) {
    let mut buf = Vec::with_capacity(estimated);
    let _ = io::copy(&mut input, &mut buf).unwrap();
    if buf.last() != Some(&b'\n') {
        buf.push(b'\n');
    }
    SCANNED.with(|scanned| scanned.borrow_mut().buf = buf);
}

fn r<T: FromStr>() -> T
where
    T::Err: fmt::Debug,
{
    SCANNED.with(|scanned| scanned.borrow_mut().next())
}

#[allow(dead_code)]
fn rv<T: FromStr>(n: usize) -> Vec<T>
where
    T::Err: fmt::Debug,
{
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..n).map(|_| scanned.next()).collect()
    })
}

#[allow(dead_code)]
fn rp<T1: FromStr, T2: FromStr>(n: usize) -> Vec<(T1, T2)>
where
    T1::Err: fmt::Debug,
    T2::Err: fmt::Debug,
{
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..n).map(|_| (scanned.next(), scanned.next())).collect()
    })
}

#[allow(dead_code)]
fn rt<T1: FromStr, T2: FromStr, T3: FromStr>(n: usize) -> Vec<(T1, T2, T3)>
where
    T1::Err: fmt::Debug,
    T2::Err: fmt::Debug,
    T3::Err: fmt::Debug,
{
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..n)
            .map(|_| (scanned.next(), scanned.next(), scanned.next()))
            .collect()
    })
}

#[allow(dead_code)]
fn rq<T1: FromStr, T2: FromStr, T3: FromStr, T4: FromStr>(n: usize) -> Vec<(T1, T2, T3, T4)>
where
    T1::Err: fmt::Debug,
    T2::Err: fmt::Debug,
    T3::Err: fmt::Debug,
    T4::Err: fmt::Debug,
{
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..n)
            .map(|_| {
                (
                    scanned.next(),
                    scanned.next(),
                    scanned.next(),
                    scanned.next(),
                )
            }).collect()
    })
}

#[allow(dead_code)]
fn rm<T: FromStr>(m: usize, n: usize) -> Vec<Vec<T>>
where
    T::Err: fmt::Debug,
{
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..m)
            .map(|_| (0..n).map(|_| scanned.next()).collect())
            .collect()
    })
}

#[allow(dead_code)]
fn r_char_mat<T, F: FnMut(u8) -> T>(h: usize, mut f: F) -> Vec<Vec<T>> {
    SCANNED.with(|scanned| {
        let mut scanned = scanned.borrow_mut();
        (0..h)
            .map(|_| {
                let l = scanned.next::<String>();
                l.as_bytes().iter().cloned().map(&mut f).collect()
            }).collect()
    })
}

#[derive(Default)]
struct Scanned {
    buf: Vec<u8>,
    pos: usize,
}

impl Scanned {
    #[inline]
    fn next<T: FromStr>(&mut self) -> T
    where
        T::Err: fmt::Debug,
    {
        let mut start = None;
        loop {
            match (self.buf[self.pos], start.is_some()) {
                (b' ', true) | (b'\n', true) => break,
                (_, true) | (b' ', false) | (b'\n', false) => self.pos += 1,
                (_, false) => start = Some(self.pos),
            }
        }
        let target = &self.buf[start.unwrap()..self.pos];
        unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
    }
}