Problem: Expression Mining

Consider an arithmetic expression built by combining single-digit positive integers with addition symbols `+`, multiplication symbols `*`, and parentheses `(` `)`, defined by the following grammar rules with the start symbol `E`.


E ::= T | E '+' T
T ::= F | T '*' F
F ::= '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '(' E ')'


When such an arithmetic expression is viewed as a string, its substring, that is, a contiguous sequence of characters within the string, may again form an arithmetic expression. Given an integer n and a string s representing an arithmetic expression, let us count the number of its substrings that can be read as arithmetic expressions with values computed equal to n.

Input

The input consists of multiple datasets, each in the following format.

> n
>  s
>

A dataset consists of two lines. In the first line, the target value n is given. n is an integer satisfying 1 ≤ n ≤ 109. The string s given in the second line is an arithmetic expression conforming to the grammar defined above. The length of s does not exceed 2×106. The nesting depth of the parentheses in the string is at most 1000.

The end of the input is indicated by a line containing a single zero. The sum of the lengths of s in all the datasets does not exceed 5×106.

Output

For each dataset, output in one line the number of substrings of s that conform to the above grammar and have the value n. The same sequence of characters appearing at different positions should be counted separately.

Sample Input


3
(1+2)*3+3
2
1*1*1+1*1*1
587
1*(2*3*4)+5+((6+7*8))*(9)
0


Output for the Sample Input


4
9
2






Example

Input

3
(1+2)*3+3
2
1*1*1+1*1*1
587
1*(2*3*4)+5+((6+7*8))*(9)
0


Output

4
9
2

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 2000005

struct Info{

	Info(){

		value = num_1 = 0;
		op = '@';
		is_number = false;
		left_index = 0;
		right_index = 0;
	}

	bool operator<(const struct Info &arg) const{

		return value < arg.value;
	}


	ll value,num_1;

	char op;
	bool is_number;
	int left_index,right_index;
};


ll value,ans;
map<pair<int,int>,ll> MAP;
char line[SIZE];

void add_Info(Info &A, Info B){

	A.value += B.value;
	if(A.value > BIG_NUM){
		A.value = BIG_NUM;
	}

	A.left_index = min(A.left_index,B.left_index);
	A.right_index = max(A.right_index,B.right_index);
}

void mult_Info(Info &A,Info B){

	A.value *= B.value;

	if(A.value >= BIG_NUM){
		A.value = BIG_NUM;
	}

	A.num_1 += B.num_1;
	A.left_index = min(A.left_index,B.left_index);
	A.right_index = max(A.right_index,B.right_index);
}


bool is_number(char ch){

	return ch >= '0' && ch <= '9';
}

ll recursive(int left,int right){

	if(MAP.count(make_pair(left,right)) > 0){

		return MAP[make_pair(left,right)];
	}

	vector<Info> V;

	Info info;

	for(int R =left; R <= right;){

		if(line[R] != '1'){

			if(is_number(line[R])){

				info.value = (line[R]-'0');
				info.num_1 = 0;
				info.is_number = true;

			}else{
				info.is_number = false;
				info.op = line[R];
			}
			info.left_index = R;
			info.right_index = R;

			V.push_back(info);
			R++;

		}else{

			info.value = 1;
			info.num_1 = 1;
			info.is_number = true;

			while(R <= right-2 && line[R+1] == '*' && line[R+2] == '1'){

				info.num_1++;
				R += 2;
			}
			info.left_index = R;
			info.right_index = R;

			V.push_back(info);
			R++;
		}
	}

	ll count;

	Info ret,tmp,op1,work,work2,calc;

	stack<Info> INFO;
	stack<Info> OP;

	bool numFLG;

	numFLG = false;

	for(int k = 0; k < V.size(); k++){

		if(V[k].is_number){

			numFLG = true;
			tmp.value = V[k].value;
			tmp.num_1 = V[k].num_1;
			tmp.left_index = V[k].left_index;
			tmp.right_index = V[k].right_index;
			tmp.is_number = true;

			if(k == V.size()-1){

				if(tmp.value == 1 && OP.empty() == false && (OP.top().op == '*') && INFO.top().value == 1){

					OP.pop();
					op1 = INFO.top();
					INFO.pop();

					info.value = tmp.value;
					info.num_1 = tmp.num_1;
					info.is_number = true;
					info.left_index = tmp.left_index;
					info.right_index = tmp.right_index;

					mult_Info(info,op1);

					INFO.push(info);

				}else{

					INFO.push(tmp);
				}
			}

		}else{

			if(numFLG){

				if(tmp.value == 1 && OP.empty() == false && (OP.top().op == '*') && INFO.top().value == 1){

					OP.pop();
					op1 = INFO.top();
					INFO.pop();

					info.value = tmp.value;
					info.num_1 = tmp.num_1;
					info.is_number = true;
					info.left_index = tmp.left_index;
					info.right_index = tmp.right_index;

					mult_Info(info,op1);

					INFO.push(info);

				}else{

					INFO.push(tmp);
				}
				numFLG = false;
			}

			if(V[k].is_number == false && (V[k].op == '+' || V[k].op == '*'|| V[k].op == '(')){

				info.is_number = false;
				info.op = V[k].op;
				info.left_index = V[k].left_index;
				info.right_index = V[k].right_index;

				OP.push(info);

			}else if(V[k].is_number == false && V[k].op == ')'){

				INFO.pop();

				ll range_value;

				while(!OP.empty()){
					if(OP.top().op == '('){
						range_value = recursive(OP.top().right_index+1,V[k].left_index-1);
						info.left_index = OP.top().right_index;
						info.right_index = V[k].left_index;
						OP.pop();
						break;
					}
					OP.pop();
					INFO.pop();
				}

				info.value = range_value;
				info.is_number = true;

				if(range_value == 1){
					info.num_1 = 1;
				}else{
					info.num_1 = 0;
				}


				INFO.push(info);
			}
		}
	}


	vector<Info> MID;

	MID.push_back(INFO.top());
	INFO.pop();


	while(!OP.empty()){

		info.is_number = false;
		info.op = OP.top().op;
		OP.pop();

		MID.push_back(info);
		MID.push_back(INFO.top());

		INFO.pop();
	}

	for(int k = 0; k < MID.size(); k++){

		if(MID[k].is_number){

			numFLG = true;
			tmp.value = MID[k].value;
			tmp.num_1 = MID[k].num_1;
			tmp.left_index = MID[k].left_index;
			tmp.right_index = MID[k].right_index;
			tmp.is_number = true;

			if(k == MID.size()-1){

				if(tmp.value == 1 && OP.empty() == false && (OP.top().op == '*') && INFO.top().value == 1){

					OP.pop();
					op1 = INFO.top();
					INFO.pop();

					info.value = tmp.value;
					info.num_1 = tmp.num_1;
					info.is_number = true;
					info.left_index = tmp.left_index;
					info.right_index = tmp.right_index;

					mult_Info(info,op1);

					INFO.push(info);

				}else{

					INFO.push(tmp);
				}
			}

		}else{

			if(numFLG){

				if(tmp.value == 1 && OP.empty() == false && (OP.top().op == '*') && INFO.top().value == 1){

					OP.pop();
					op1 = INFO.top();
					INFO.pop();

					info.value = tmp.value;
					info.num_1 = tmp.num_1;
					info.is_number = true;
					info.left_index = tmp.left_index;
					info.right_index = tmp.right_index;

					mult_Info(info,op1);

					INFO.push(info);

				}else{

					INFO.push(tmp);
				}
				numFLG = false;
			}

			if(MID[k].is_number == false && (MID[k].op == '+' || MID[k].op == '*'|| MID[k].op == '(')){

				info.is_number = false;
				info.op = MID[k].op;
				info.left_index = MID[k].left_index;
				info.right_index = MID[k].right_index;

				OP.push(info);
			}
		}
	}

	vector<Info> FINAL;

	FINAL.push_back(INFO.top());
	INFO.pop();

	while(!OP.empty()){

		info.is_number = false;
		info.op = OP.top().op;
		OP.pop();

		FINAL.push_back(info);
		FINAL.push_back(INFO.top());

		INFO.pop();
	}

	int num_term = 0;
	vector<int> term_index,term_value;

	for(int i = 0; i < FINAL.size(); i++){
		if(FINAL[i].is_number == false && FINAL[i].op == '+'){

			term_index.push_back(++num_term);
		}else{
			term_index.push_back(num_term);
		}
	}

	info.value = 0;
	info.num_1 = 0;

	work.value = 0;
	work.num_1 = 0;

	int L = 0,R = 0;
	bool pre_R = true;

	char last_op = '@';
	bool move_L;

	while(L <= FINAL.size()-1){

		if(L > R){
			R = L;
			last_op = '@';
			info.value = 0;
			pre_R = true;
		}

		if(FINAL[R].is_number){

			if(pre_R){

				if(last_op == '@' || last_op == '+'){

					work = FINAL[R];

				}else{ //last_op = '*'

					work.value = max(1LL,work.value)*FINAL[R].value;
				}

				pre_R = false;

			}else{

				if(!FINAL[L].is_number){

					L++;
					pre_R = false;
					continue;

				}else{ //FINAL[L].is_number

					//Do nothing
				}
			}

		}else{ //FINAL[R].is_number == false

			if(FINAL[R].op == '+'){

				if(pre_R){
					term_value.push_back(work.value);
					info.value += work.value;
					work.value = 0;
				}
				last_op = '+';

			}else{ //FINAL[R].op == '*'

				last_op = '*';
			}
			R++;
			pre_R = true;
			continue;
		}

		move_L = false;

		if((info.value+work.value) <= value){


			if((info.value+work.value) == value){

				if(L == R){

					if(value == 1){

						ans += max(1LL,(FINAL[L].num_1*(FINAL[L].num_1-1))/2+FINAL[L].num_1);

					}else{
						ans += max(1LL,FINAL[L].num_1);
					}

					if(R == FINAL.size()-1)break;

					R++;
					pre_R = true;
					move_L = false;

				}else{ //L != E

					ans += max(1LL,FINAL[L].num_1)*max(1LL,FINAL[R].num_1);

					if(R+2 <= FINAL.size()-1 && FINAL[R+1].op == '*' && FINAL[R+2].value == 1){

						ans +=max(1LL,FINAL[L].num_1)*max(1LL,FINAL[R+2].num_1);

					}
					move_L = true;
				}

			}else{ //(info.value+work.value) < value)

				if(R == FINAL.size()-1)break;

				R++;
				move_L = false;
				pre_R = true;
			}

		}else{ //(info.value+work.value) > value

			if(L != R){

				move_L = true;
			}else{

				if(L == FINAL.size()-1 && R == FINAL.size()-1)break;
				R++;
				pre_R = true;
				move_L = false;
			}
		}

		if(move_L){

			if(term_index[L] == term_index[R]){

				if(L == R){

					info.value = 0;
					info.num_1 = 0;

					work.value = 0;
					work.num_1 = 0;

					term_value[term_index[R]] = 0;

				}else{

					work.value /= FINAL[L].value;
				}

			}else{

				info.value -= term_value[term_index[L]];

				if(L <= FINAL.size()-2 && term_index[L] != term_index[L+1]){
					term_value[term_index[L]] = 0;
				}else{
					term_value[term_index[L]] /= FINAL[L].value;
				}
				info.value += term_value[term_index[L]];
			}

			L++;
			pre_R = false;
		}
	}

	info.value = 0;

	while(!OP.empty())OP.pop();
	while(!INFO.empty())INFO.pop();

	for(int i = 0; i < FINAL.size(); i++){

		if(FINAL[i].is_number){

			info.value = FINAL[i].value;

			if(OP.empty() == false && OP.top().op == '*'){
				OP.pop();
				mult_Info(info,INFO.top());
				INFO.pop();

				INFO.push(info);

			}else{

				INFO.push(info);
			}

		}else{

			OP.push(FINAL[i]);
		}
	}

	ret.value = 0;

	while(!INFO.empty()){
		add_Info(ret,INFO.top());
		INFO.pop();
	}

	return MAP[make_pair(left,right)] = ret.value;
}

int main(){

	while(true){
		scanf("%lld",&value);
		if(value == 0)break;

		MAP.clear();

		scanf("%s",line);

		int length;

		ans = 0;
		for(length = 0; line[length] != '\0'; length++);

		recursive(0,length-1);

		printf("%lld\n",ans);
	}

	return 0;
}