1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| string mul(string str, int x) { string res(str.size(), ' '); int carry = 0; for (int i = res.size() - 1; i >= 0; --i) { int cur = (str[i] - '0') * x + carry; str[i] = cur / 10 + '0'; carry = cur % 10; } if (carry) return to_string(carry) + res; else return res; }
string mul(string a, string b) { string res = "0"; for (int i = 0; i < b.size(); ++i) { int x = b[b.size() - 1 - i] - '0'; string cur = mul(a, x); for (int j = 0; j < i; ++i) cur = mul(cur, 10); res = add(res, cur); } return res; }
|