#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
/*
Multiply Two Long Numbers
------------------------------------
Implements manual big‑integer multiplication
using decimal strings.
*/
std::string longmulti(const std::string& A, const std::string& B) {
std::string a = A;
std::string b = B;
// Trim leading spaces
a.erase(0, a.find_first_not_of(' '));
b.erase(0, b.find_first_not_of(' '));
// Handle sign
bool sign = false;
if (!a.empty() && a[0] == '-') { sign = !sign; a.erase(0, 1); }
if (!b.empty() && b[0] == '-') { sign = !sign; b.erase(0, 1); }
// Remove leading zeros
a.erase(0, a.find_first_not_of('0'));
b.erase(0, b.find_first_not_of('0'));
if (a.empty()) a = "0";
if (b.empty()) b = "0";
// If either is zero
if (a == "0" || b == "0")
return "0";
int la = a.size();
int lb = b.size();
std::string result(la + lb, '0');
// Multiply from right to left
for (int i = la - 1; i >= 0; --i) {
if (!std::isdigit(a[i])) continue;
int carry = 0;
for (int j = lb - 1; j >= 0; --j) {
if (!std::isdigit(b[j])) continue;
int pos = i + j + 1;
int n = (a[i] - '0') * (b[j] - '0')
+ (result[pos] - '0') + carry;
result[pos] = char('0' + (n % 10));
carry = n / 10;
}
result[i] = char(result[i] + carry);
}
// Remove leading zero if present
if (result[0] == '0')
result.erase(0, 1);
// Add sign
if (sign)
result.insert(result.begin(), '-');
return result;
}
int main() {
std::string r = longmulti(
" 18361891827367132321",
"-18361891827367132321"
);
std::cout << r << '\n';
}
/*
run:
-337159071479931885857929667075122847041
*/