#include<iostream> #include<string> #include<vector> #include<algorithm> #include<iomanip> using namespace std; constexpr long long E18 = 1e18; struct BigNum { int n; vector<long long> a; BigNum() { string buffer; cin >> buffer; reverse(buffer.begin(), buffer.end()); int l = buffer.size(); for (int i = 0; i < l; i += 18) { string sub = buffer.substr(i, 18); reverse(sub.begin(), sub.end()); a.push_back(stoll(sub)); } n = a.size(); } int size() const { return n; } BigNum& operator+=(const BigNum& other) { int m = other.size(); for (int i = 0; i < m; ++i) a[i] += other.a[i]; for (int i = 0; i < n - 1; ++i) { a[i + 1] += a[i] / E18; a[i] %= E18; } if (a[n - 1] >= E18) { a.push_back(a[n - 1] / E18); a[n - 1] %= E18; ++n; } return *this; } friend ostream& operator<<(ostream& os, const BigNum& bn) { os << bn.a[bn.n - 1]; for (int i = bn.n - 2; i >= 0; --i) { os << setw(18) << setfill('0') << bn.a[i]; } return os; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); BigNum a, b; if (a.size() < b.size()) swap(a, b); a += b; cout << a; }
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include<iostream> #include<string> #include<vector> #include<algorithm> #include<iomanip> using namespace std; constexpr long long E18 = 1e18; struct BigNum { int n; vector<long long> a; BigNum() { string buffer; cin >> buffer; reverse(buffer.begin(), buffer.end()); int l = buffer.size(); for (int i = 0; i < l; i += 18) { string sub = buffer.substr(i, 18); reverse(sub.begin(), sub.end()); a.push_back(stoll(sub)); } n = a.size(); } int size() const { return n; } BigNum& operator+=(const BigNum& other) { int m = other.size(); for (int i = 0; i < m; ++i) a[i] += other.a[i]; for (int i = 0; i < n - 1; ++i) { a[i + 1] += a[i] / E18; a[i] %= E18; } if (a[n - 1] >= E18) { a.push_back(a[n - 1] / E18); a[n - 1] %= E18; ++n; } return *this; } friend ostream& operator<<(ostream& os, const BigNum& bn) { os << bn.a[bn.n - 1]; for (int i = bn.n - 2; i >= 0; --i) { os << setw(18) << setfill('0') << bn.a[i]; } return os; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); BigNum a, b; if (a.size() < b.size()) swap(a, b); a += b; cout << a; } |