Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Bigint
- {
- static const int max = 3000;
- int *n;
- int s;
- int sign;
- public:
- Bigint()
- {
- n = new int[max];
- s = 0;
- sign = 1;
- for (int i = 0; i < max; i++)
- n[i] = 0;
- }
- Bigint(int size)
- {
- n = new int[max];
- s = size;
- sign = 1;
- for (int i = 0; i < max; i++)
- n[i] = 0;
- }
- Bigint(char *str, int size)
- {
- n = new int[max];
- s = size;
- int i;
- sign = 1;
- for (i = 0; i < s; i++)
- {
- n[s - 1 - i] = str[i] - 48;
- }
- for (; i < max; i++)
- n[i] = 0;
- }
- Bigint(const Bigint &other)
- {
- if (this != &other)
- {
- n = new int[max];
- s = other.s;
- sign = other.sign;
- for (int i = 0; i < max; i++)
- n[i] = other.n[i];
- }
- }
- ~Bigint()
- {
- //delete[] n;
- }
- Bigint& operator=(const Bigint &other)
- {
- if (this != &other)
- {
- n = new int[max];
- s = other.s;
- sign = other.sign;
- for (int i = 0; i < max; i++)
- n[i] = other.n[i];
- }
- return *this;
- }
- Bigint& operator=(Bigint&& other)
- {
- s = other.s;
- sign = other.sign;
- //delete[] n;
- n = other.n;
- other.n = NULL;
- return *this;
- }
- friend ostream &operator<<(ostream &output, const Bigint &an)
- {
- if (an.sign == -1) output << '-';
- for (int i = an.s - 1; i >= 0; i--)
- {
- output << an.n[i];
- }
- output << endl;
- return output;
- }
- friend istream &operator>>(istream &input, Bigint &an)
- {
- char *str = new char[max];
- int i = 0, j = 0;
- input >> str;
- an.s = strlen(str);
- if (str[0] == '-') { an.sign = -1; i++; an.s -= 1; }
- for (j = 0; j < an.s; i++, j++)
- {
- an.n[an.s - 1 - j] = str[i] - 48;
- }
- for (; j <= max; j++)
- an.n[j] = 0;
- delete [] str;
- return input;
- }
- bool operator==(Bigint an)
- {
- if (sign != an.sign || s != an.s) return false;
- for (int i = 0; i < s; i++)
- {
- if (n[i] != an.n[i]) return false;
- }
- return true;
- }
- inline bool operator!=(Bigint an) { return !(*this == an); }
- bool operator>(Bigint an)
- {
- if (sign == 1 && an.sign == -1) return true;
- else if (sign == -1 && an.sign == 1) return false;
- else
- {
- bool rez = false;
- if (s > an.s) return true;
- else if (s < an.s) return false;
- for (int i = 0; i < s; i++)
- {
- if (n[i] > an.n[i])
- {
- rez = true;
- break;
- }
- }
- if (sign == -1) rez = !rez;
- return rez;
- }
- return false;
- }
- inline bool operator<(Bigint an) { return an > *this; }
- inline bool operator<=(Bigint an) { return !(*this>an); }
- inline bool operator>=(Bigint an) { return !(*this<an); }
- Bigint operator+(Bigint an)
- {
- Bigint rez = *this;
- int m = s > an.s ? s : an.s, c = 0;
- // a + b
- if (sign == 1 && an.sign == 1)
- {
- for (int i = 0; i < m; i++)
- {
- c += n[i] + an.n[i];
- n[i] = c % 10;
- c = c / 10;
- }
- s = m;
- if (c > 0)
- {
- n[m] = c;
- s = m + 1;
- }
- }
- //-a + -b = -(a + b)
- else if (sign == -1 && an.sign == -1)
- {
- an.sign = 1;
- sign = 1;
- rez = an + *this;
- rez.sign = -1;
- *this = rez;
- }
- else if (sign == 1 && an.sign == -1)
- {
- an.sign = 1;
- *this = rez - an;
- }
- else
- {
- rez.sign = 1;
- *this = an - rez;
- }
- return *this;
- }
- Bigint operator-(Bigint an)
- {
- Bigint rez = *this;
- //a - -b = a + b
- if (sign == 1 && an.sign == -1)
- {
- an.sign = 1;
- rez = *this + an;
- }
- //-a - b = -a + -b
- else if (sign == -1 && an.sign == 1)
- {
- an.sign = -1;
- rez = *this + an;
- }
- //a - b
- else if (sign == 1 && an.sign == 1)
- {
- if (rez < an) {
- rez = an - rez;
- rez.sign = -1;
- }
- else
- {
- int c = 0;
- for (int i = 0; i < rez.s; i++)
- {
- c = c + rez.n[i] - an.n[i] + 10;
- rez.n[i] = c % 10;
- if (c < 10) c = -1;
- else c = 0;
- }
- while (rez.n[rez.s - 1] == 0 && rez.s > 1) rez.s--;
- }
- }
- else
- {
- an.sign = 1;
- rez.sign = 1;
- rez = an - rez;
- }
- *this = rez;
- return *this;
- }
- Bigint operator*(Bigint an)
- {
- Bigint rez = Bigint();
- int cr = 0, k;
- if (sign != an.sign) rez.sign = -1;
- for (int i = 0; i < s; i++)
- {
- for (int j = 0; j < an.s; j++)
- {
- cr = n[i] * an.n[j];
- k = i + j;
- while (cr>0)
- {
- cr += rez.n[k];
- rez.n[k] = cr % 10;
- cr = cr / 10;
- if (k + 1>rez.s) rez.s = k + 1;
- k += 1;
- }
- }
- }
- return rez;
- }
- Bigint operator/(long an)
- {
- Bigint rez = Bigint();
- int ch = 0, k = 0;
- for (int i = 0; i < s; i++)
- {
- ch = ch * 10 + n[s - i - 1];
- if (ch < an && k == 0 && i > 1) continue;
- k = 1;
- if (ch / an != 0) {
- rez.n[s - i - 1] = ch / an;
- rez.s++;
- }
- ch = ch % an;
- }
- return rez;
- }
- long operator%(long an)
- {
- long r = 0;
- for (int i = 0; i < s; i++)
- {
- r = (r * 10 + n[s-i-1]) % an;
- }
- return r;
- }
- bool isZero()
- {
- return (s == 1 && n[0] == 0) ? true : false;
- }
- Bigint pow(int n)
- {
- Bigint r = *this;
- if (n == 0)
- {
- Bigint zero = Bigint("1", 1);
- return zero;
- }
- for (int i = 1; i < n; i++)
- {
- r = r * *this;
- }
- return r;
- }
- };
- void main()
- {
- Bigint n1, n2;
- int i1;
- long l1;
- cin >> n1;
- cin >> l1;
- //cout << (n1 < n2 ? "true" : "false") << endl;
- //n1 = n1 / i1;
- l1 = n1 % l1;
- cout << l1 << endl;
- cin.ignore();
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment