void i2s(int n, string &str){
stringstream stream;
stream <<n;
stream>>str;
}
int cmp(string a, string b){
if(a.find_first_not_of('0') == string::npos) a = '0';
else a.substr(a.find_first_not_of('0'));
if(b.find_first_not_of('0') == string::npos) b = '0';
else b.substr(b.find_first_not_of('0'));
if(a.length() > b.length()) return 1;
else if(a.length() < b.length()) return -1;
else{
if(a > b) return 1;
if(a < b) return -1;
else return 0;
}
}
string add(string a,string b){
a = a.substr(a.find_first_not_of('0'));
b = b.substr(b.find_first_not_of('0'));
long long lenA = a.length();
long long lenB = b.length();
long long len = max(lenA,lenB) + 10;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string ans(len,'0');
for(int i = 0; i < lenA; i++){
ans[i] = a[i];
}
int temp = 0;
for(int i = 0; i < len; ++i){
if(i < b.length())
temp += (ans[i] - '0') + (b[i] - '0');
else
temp += (ans[i] - '0');
ans[i] = temp % 10 + '0';
temp /= 10;
}
reverse(ans.begin(),ans.end());
return ans.substr(ans.find_first_not_of('0'));
}
string subtract(string a,string b){
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for(int i = 0; i < b.length(); i++){
if(a[i] >= b[i]){
a[i] = a[i] - b[i] + '0';
}else{
int next = 1;
while(a[i + next] == '0') {
a[i+next] = '9';
next++;
}
a[i+next] = a[i+next] - '1' + '0';
a[i] = (a[i]-'0'+10) - (b[i]-'0')+ '0';
}
}
reverse(a.begin(), a.end());
if(a.find_first_not_of('0') == string::npos) return "0";
return a.substr(a.find_first_not_of('0'));
}
std::string multiply(std::string a, std::string b){
std::string result = "";
int row = b.size();
int col = a.size() + 1;
int tmp[row][col];
memset(tmp,0, sizeof(int)*row*col);
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(int i = 0; i < b.size(); i++){
for(int j = 0; j < a.size(); j++){
std::string bit_a = std::string(1, a.at(j));
std::string bit_b = std::string(1, b.at(i));
tmp[i][j] += std::stoi(bit_a) * std::stoi(bit_b);
tmp[i][j+1] = tmp[i][j] / 10;
tmp[i][j] %= 10;
}
}
int N = a.size() + b.size();
int sum[N];
memset(sum, 0, sizeof(int)*N);
for(int n = 0; n < N; n++){
int i = 0;
int j = n;
while (i <= n && j >= 0 ){
if(i < row && j < col){
sum[n] += tmp[i][j];
}
i++;
j--;
}
if( n+1 < N ){
sum[n+1] = sum[n] / 10;
sum[n] %= 10;
}
}
bool zeroStartFlag = true;
for (int i = N-1; i >= 0; i--){
if(sum[i]==0 && zeroStartFlag){
continue;
}
zeroStartFlag = false;
result.append(std::to_string(sum[i]));
}
return result;
}
string divide(string a,string b){
string ans = "0.";
for(int i = 0; i < 101; i++){
a.append("0");
int t = 0;
while(cmp(a,b) >= 0){
a = subtract(a,b);
t++;
}
string t_str;
i2s(t, t_str);
ans.append(t_str);
}
return ans;
}