tuki2501

simple bignum for c++

Oct 7th, 2020 (edited)
1,776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. const int BASE = 1e9;
  2.  
  3. struct Int {
  4.   vector<int> v;
  5.   Int(int x = 0) {
  6.     if (x == 0) {
  7.       v.resize(1);
  8.       return;
  9.     }
  10.     while (x) {
  11.       v.push_back(x % BASE);
  12.       x /= BASE;
  13.     }
  14.   }
  15.   Int(string s) {
  16.     reverse(all(s));
  17.     while (s.size() % 9) s += '0';
  18.     reverse(all(s));
  19.     for (int i = s.size() - 9; i >= 0; i -= 9) {
  20.       v.push_back(stoi(s.substr(i, 9)));
  21.     }
  22.   }
  23.   void Rlz() {
  24.     while (v.size() > 1 && v.back() == 0) v.pop_back();
  25.   }
  26. };
  27.  
  28. Int operator+(Int a, Int b) {
  29.   while ((int)(a.v.size()) < (int)(b.v.size())) a.v.push_back(0);
  30.   while ((int)(a.v.size()) > (int)(b.v.size())) b.v.push_back(0);
  31.   Int c; c.v.resize(max((int)(a.v.size()), (int)(b.v.size())));
  32.   int g = 0;
  33.   for (int i = 0; i < (int)(c.v.size()); i++) {
  34.     if (i < (int)(a.v.size())) g += a.v[i];
  35.     if (i < (int)(b.v.size())) g += b.v[i];
  36.     c.v[i] = g % BASE;
  37.     g /= BASE;
  38.   }
  39.   if (g > 0) c.v.push_back(1);
  40.   c.Rlz();
  41.   return c;
  42. }
  43.  
  44. Int operator*(Int a, Int b) {
  45.   Int c; c.v.resize((int)(a.v.size()) + (int)(b.v.size()));
  46.   for (int i = 0; i < (int)(a.v.size()); i++) {
  47.     int g = 0;
  48.     for (int j = 0; j < (int)(b.v.size()); j++) {
  49.       g += c.v[i + j] + a.v[i] * b.v[j];
  50.       c.v[i + j] = g % BASE;
  51.       g /= BASE;
  52.     }
  53.     if (g > 0) c.v[i + (int)(b.v.size())] += g;
  54.   }
  55.   c.Rlz();
  56.   return c;
  57. }
  58.  
  59. Int operator-(Int a, Int b) {
  60.   Int c; c.v.resize((int)(a.v.size()));
  61.   while ((int)(a.v.size()) > (int)(b.v.size())) b.v.push_back(0);
  62.   int g = 0;
  63.   for (int i = 0; i < (int)(a.v.size()); i++) {
  64.     g += a.v[i] - b.v[i];
  65.     if (g >= 0) c.v[i] = g, g = 0;
  66.     else c.v[i] = g + BASE, g = -1;
  67.   }
  68.   c.Rlz();
  69.   return c;
  70. }
  71.  
  72. bool operator<(Int a, Int b) {
  73.   if ((int)(a.v.size()) != (int)(b.v.size())) {
  74.     return (int)(a.v.size()) < (int)(b.v.size());
  75.   }
  76.   for (int i = (int)((int)(a.v.size())) - 1; i >= 0; i--) {
  77.     if (a.v[i] != b.v[i]) return a.v[i] < b.v[i];
  78.   }
  79.   return 0;
  80. }
  81.  
  82. bool operator==(Int a, Int b) {
  83.   if ((int)(a.v.size()) != (int)(b.v.size())) return 0;
  84.   for (int i = 0; i < (int)(a.v.size()); i++) {
  85.     if (a.v[i] != b.v[i]) return 0;
  86.   }
  87.   return 1;
  88. }
  89.  
  90. ostream& operator<<(ostream& os, Int x) {
  91.   os << x.v.back();
  92.   for (int i = (int)(x.v.size()) - 2; i >= 0 ; i--) {
  93.     cout << setw(9) << setfill('0') << x.v[i];
  94.   }
  95.   return os;
  96. }
  97.  
  98. istream& operator>>(istream& is, Int& x) {
  99.   string s;
  100.   cin >> s;
  101.   x = Int(s);
  102.   return is;
  103. }
  104.  
Add Comment
Please, Sign In to add comment