0% found this document useful (0 votes)
196 views2 pages

Big Number Arithmetic in C++

The document defines functions for performing arithmetic operations on large numbers represented as vectors of integers. It includes functions to convert strings to large number representations, compare two large numbers, add, subtract, multiply large numbers by integers or other large numbers, and display a large number. The main function takes in two strings as large numbers, performs the arithmetic operations on them and displays the results.

Uploaded by

Huy Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views2 pages

Big Number Arithmetic in C++

The document defines functions for performing arithmetic operations on large numbers represented as vectors of integers. It includes functions to convert strings to large number representations, compare two large numbers, add, subtract, multiply large numbers by integers or other large numbers, and display a large number. The main function takes in two strings as large numbers, performs the arithmetic operations on them and displays the results.

Uploaded by

Huy Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <bits/stdc++.

h>
using namespace std;

typedef long long ll;


typedef vector < int > bignum;
const int base = 1e9;
const int digit = 9;

inline bignum str2big(string s)


{
while(s.size() % digit) s = '0' + s;
bignum res;
for(int i = s.size() - 1; i >= 0; i -= digit)
{
ll t = 0;
for(int j = i - digit + 1; j <= i; j++)
t = t * 10 + s[j] - 48;
res.push_back(t);
}
return res;
}

inline bool operator > (bignum x, bignum y)


{
if(x.size() != y.size()) return (x.size() > y.size());
for(int i = x.size() - 1; i >= 0; i--)
if(x[i] != y[i])
return (x[i] > y[i]);
return false;
}

inline bool operator < (bignum x, bignum y)


{
return (y > x);
}

inline bignum operator + (bignum x, bignum y)


{
for(int i = 0, cur = 0; i < max(x.size(), y.size()) || cur; i++)
{
if(i == x.size()) x.push_back(0);
x[i] = x[i] + (i < y.size() ? y[i] : 0) + cur;
cur = (x[i] >= base);
if(cur) x[i] -= base;
}
if(x.size() > 1 && !x.back()) x.pop_back();
return x;
}

inline bignum operator - (bignum x, bignum y)


{
for(int i = 0, cur = 0; i < x.size() || cur; i++)
{
x[i] = x[i] - (i < y.size() ? y[i] : 0) - cur;
cur = (x[i] < 0);
if(cur) x[i] += base;
}
while(x.size() > 1 && !x.back()) x.pop_back();
return x;
}

inline bignum operator * (bignum P, ll k)


{
for(int i = 0, cur = 0; i < P.size() || cur; i++)
{
if(i == P.size()) P.push_back(0);
ll t = (ll)(P[i] * k + cur);
cur = (ll)(t / base);
P[i] = (ll)(t % base);
}
while(P.size() > 1 && !P.back()) P.pop_back();
return P;
}

inline bignum operator * (bignum P, bignum Q)


{
bignum res, tmp;
for(int i = 0; i < P.size() + Q.size(); i++)
res.push_back(0);
for(int i = 0; i < P.size(); i++)
{
tmp = Q * P[i];
tmp.insert(tmp.begin(), i, 0);
res = res + tmp;
}
while(res.size() > 1 && !res.back()) res.pop_back();
return res;
}
inline void display(bignum P)
{
if(P.empty()) cout << 0;
else cout << P.back();
for(int i = P.size() - 2; i >= 0; i--)
cout << setw(digit) << setfill('0') << P[i];
cout << '\n';
}

string s, t;
bignum a, b;

int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> s >> t;
a = str2big(s);
b = str2big(t);
display(a + b);
if(a < b)
{
cout << '-';
display(b - a);
}
else display(a - b);
display(a * b);
}

You might also like