#include #include #include #include #include #include using namespace std; int type_digit = 0; int type_char = 1; const long long infinity = 1000 * 1000 + 10; struct Expression{ int type; int d; Expression * child; char c; Expression * successor; long long length() const { if(type == type_char){ if(successor){ return min(infinity, successor->length() + 1); } return 1; }else{ long long length = child->length() * d; if(length > infinity){return infinity;} if(successor){ length += successor->length(); } return min(infinity, length); } } char f(int i) const { //cout << "i = " << i << endl; //cout.flush(); if(type == type_char){ if(i == 0){return c;} if(successor){ return successor->f(i - 1); } }else{ long long length = child->length(); if(length >= infinity){length = infinity;} if(length == infinity){ return child->f(i); } length *= d; if(length > i){ return child->f(i % child->length()); } if(successor){ return successor->f(i - length); } } return '0'; } }; Expression * expression(const char * & p){ if(p[0] == '\0' or p[0] == ')'){ return 0; }else if(isdigit(p[0])){ int k = strtol(p, (char **)&p, 10); Expression *e = new Expression(); if(p[0] == '('){ p++; e->child = expression(p); p++; }else{ e->child = new Expression(); e->child->type = type_char; e->child->c = p[0]; e->child->successor = 0; p++; } e->type = type_digit; e->d = k; e->successor = expression(p); return e; }else{ Expression *e = new Expression(); e->c = p[0]; e->type = type_char; e->successor = expression(++p); return e; } } int main(){ while(true){ string s; int i; cin >> s >> i; if(s == "0" and i == 0){break;} const char *p = s.c_str(); //cout << "p = " << p << endl; Expression * e = expression(p); //cout << "p = " << p << endl; //cout << "e = " << e << endl; cout << e->f(i) << endl; //cout << e->c << endl; } }