Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <math.h>
- #include <cstdio>
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- class BigIntegerOverflow : std::exception{
- };
- class BigInt {
- friend ostream& operator <<(ostream&, const BigInt&);
- friend istream& operator >>(istream&, BigInt&);
- friend BigInt operator / (const BigInt&, const BigInt&);
- friend BigInt operator % (const BigInt&, const BigInt&);
- friend BigInt big_sqrt (const BigInt&);
- friend bool operator <= (const BigInt& left, const BigInt& right);
- friend bool operator >= (const BigInt& left, const BigInt& right);
- friend bool operator != (const BigInt& left, const BigInt& right);
- friend BigInt operator + (const BigInt& left, const BigInt& right);
- friend BigInt operator * (const BigInt& left, const BigInt& right);
- friend BigInt operator - (const BigInt& left, const BigInt& right);
- friend bool operator == (const BigInt& left, const BigInt& right);
- friend bool operator < (const BigInt& left, const BigInt& right);
- friend bool operator > (const BigInt& left, const BigInt& right);
- private:
- int* data;
- bool sign;
- int lenght;
- public:
- BigInt(unsigned int);
- BigInt(long long);
- BigInt(unsigned long long);
- BigInt(int);
- ~BigInt();
- BigInt(const string&);
- BigInt();
- BigInt(const BigInt&);
- BigInt& operator =(const BigInt& big_int);
- BigInt& operator++ ();
- BigInt& operator-- ();
- const BigInt operator++ (int);
- const BigInt operator-- (int);
- BigInt& operator += (const BigInt& right);
- BigInt& operator -= (const BigInt& right);
- BigInt& operator *= (const BigInt& right);
- BigInt operator - ();
- BigInt& operator + ();
- };
- int MAX_LENGTH = 50000;
- int LENGTH_LIMIT = 21000;
- void reverse_array (int* arr, int length){
- for (int i = 0; i < length / 2; ++i){
- swap(arr[i], arr[length - 1 - i]);
- }
- }
- BigInt& BigInt::operator+=(const BigInt &right) {
- *this = *this + right;
- return *this;
- }
- BigInt& BigInt::operator+() {
- return *this;
- }
- BigInt& BigInt::operator*=(const BigInt &right) {
- *this = *this * right;
- return *this;
- }
- BigInt BigInt::operator-() {
- return (*this * (-1));
- }
- bool operator != (const BigInt& left, const BigInt& right){
- return !(left == right);
- }
- bool operator <= (const BigInt& left, const BigInt& right){
- return !(left > right);
- }
- bool operator >= (const BigInt& left, const BigInt& right){
- return !(left < right);
- }
- BigInt& BigInt::operator-=(const BigInt &right) {
- *this = *this - right;
- return *this;
- }
- BigInt& BigInt::operator++() {
- *this += 1;
- return *this;
- }
- BigInt& BigInt::operator--() {
- *this -= 1;
- return *this;
- }
- const BigInt BigInt::operator++(int) {
- const BigInt temp = *this;
- *this += 1;
- return temp;
- }
- const BigInt BigInt::operator--(int) {
- const BigInt temp = *this;
- *this -= 1;
- return temp;
- }
- BigInt::BigInt() {
- data = new int[MAX_LENGTH];
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- lenght = 0;
- sign = true;
- }
- BigInt::BigInt(long long k) {
- data = new int [MAX_LENGTH];
- if (k == 0){
- data[0] = 0;
- lenght = 1;
- sign = true;
- return;
- }
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- if (k < 0){
- sign = false;
- k *= -1;
- }
- else{
- sign = true;
- }
- int digit;
- int count_of_digits = 0;
- while(k > 0){
- digit = k % 10;
- k = k / 10;
- data[count_of_digits] = digit;
- ++count_of_digits;
- }
- for (int i = 0; i < count_of_digits / 2; ++i){
- swap(data[i], data[count_of_digits - i - 1]);
- }
- lenght = count_of_digits;
- }
- BigInt::BigInt(unsigned int k) {
- data = new int [MAX_LENGTH];
- if (k == 0){
- data[0] = 0;
- lenght = 1;
- sign = true;
- return;
- }
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- if (k < 0){
- sign = false;
- k *= -1;
- }
- else{
- sign = true;
- }
- int digit;
- int count_of_digits = 0;
- while(k > 0){
- digit = k % 10;
- k = k / 10;
- data[count_of_digits] = digit;
- ++count_of_digits;
- }
- for (int i = 0; i < count_of_digits / 2; ++i){
- swap(data[i], data[count_of_digits - i - 1]);
- }
- lenght = count_of_digits;
- }
- BigInt::BigInt(unsigned long long k) {
- data = new int [MAX_LENGTH];
- if (k == 0){
- data[0] = 0;
- lenght = 1;
- sign = true;
- return;
- }
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- if (k < 0){
- sign = false;
- k *= -1;
- }
- else{
- sign = true;
- }
- int digit;
- int count_of_digits = 0;
- while(k > 0){
- digit = k % 10;
- k = k / 10;
- data[count_of_digits] = digit;
- ++count_of_digits;
- }
- for (int i = 0; i < count_of_digits / 2; ++i){
- swap(data[i], data[count_of_digits - i - 1]);
- }
- lenght = count_of_digits;
- }
- BigInt::BigInt(const string& str) {
- data = new int [MAX_LENGTH];
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- if (str[0] == '-'){
- lenght = str.size() - 1;
- if (lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- sign = false;
- for (int i = 0; i < lenght; ++i){
- data[i] = str[i + 1] - 48;
- }
- }
- else{
- sign = true;
- lenght = str.size();
- if (lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- for (int i = 0; i < lenght; ++i){
- data[i] = str[i] - 48;
- }
- }
- }
- BigInt::BigInt(int k) {
- data = new int [MAX_LENGTH];
- if (k == 0){
- data[0] = 0;
- lenght = 1;
- sign = true;
- return;
- }
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- if (k < 0){
- sign = false;
- k *= -1;
- }
- else{
- sign = true;
- }
- int digit;
- int count_of_digits = 0;
- while(k > 0){
- digit = k % 10;
- k = k / 10;
- data[count_of_digits] = digit;
- ++count_of_digits;
- }
- for (int i = 0; i < count_of_digits / 2; ++i){
- swap(data[i], data[count_of_digits - i - 1]);
- }
- lenght = count_of_digits;
- }
- istream& operator >> (istream& in, BigInt& big_int){
- string str;
- cin >> str;
- big_int = BigInt(str);
- return in;
- }
- ostream& operator << (ostream& out, const BigInt& B_INT){
- if (! B_INT.sign){
- cout << "-";
- for (int i = 0; i < B_INT.lenght; ++i){
- cout << B_INT.data[i];
- }
- }
- else{
- for (int i = 0; i < B_INT.lenght; ++i){
- cout << B_INT.data[i];
- }
- }
- return out;
- }
- BigInt::~BigInt() {
- delete[] data;
- }
- BigInt& BigInt::operator = (const BigInt& big_int){
- if (this == &big_int){
- return *this;
- }
- for (int i = 0 ; i < this->lenght; ++i){
- data[i] = 0;
- }
- lenght = big_int.lenght;
- sign = big_int.sign;
- for (int i = 0; i < lenght; ++i){
- data[i] = big_int.data[i];
- }
- return *this;
- }
- BigInt::BigInt(const BigInt& big_int) {
- data = new int [MAX_LENGTH];
- for (int i = 0; i < MAX_LENGTH; ++i){
- data[i] = 0;
- }
- lenght = big_int.lenght;
- sign = big_int.sign;
- for (int i = 0; i < lenght; ++i){
- data[i] = big_int.data[i];
- }
- }
- BigInt operator + (const BigInt& l, const BigInt& r) {
- BigInt left = l, right = r;
- BigInt result;
- if (left.sign && right.sign) {
- if (left.lenght > right.lenght) {
- result.lenght = left.lenght + 1;
- } else {
- result.lenght = right.lenght + 1;
- }
- for (int i = 0; i < result.lenght; ++i) {
- result.data[i] = 0;
- }
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- for (int i = 0; i < result.lenght - 1; ++i) {
- result.data[i] += left.data[i] + right.data[i];
- result.data[i + 1] += (result.data[i] / 10);
- result.data[i] %= 10;
- }
- if (result.data[result.lenght - 1] == 0) {
- result.lenght -= 1;
- }
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- reverse_array(result.data, result.lenght);
- result.sign = true;
- if (result.lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- return result;
- }
- else{
- if (!left.sign && right.sign) {
- BigInt temp = left;
- temp.sign = true;
- result = right - temp;
- if (result.lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- return result;
- }
- if(left.sign && !right.sign){
- BigInt temp = right;
- temp.sign = true;
- result = left - temp;
- if (result.lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- return result;
- }
- if(!left.sign && !right.sign){
- BigInt temp = right;
- temp.sign = true;
- result = left - temp;
- if (result.lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- return result;
- }
- }
- }
- bool operator ==(const BigInt& left, const BigInt & right){
- if(left.sign != right.sign){
- return false;
- }
- if(left.lenght != right.lenght){
- return false;
- }
- for (int i = 0; i < left.lenght; ++i) {
- if (left.data[i] != right.data[i]) {
- return false;
- }
- }
- return true;
- }
- bool operator< (const BigInt& left, const BigInt& right){
- if (left == right){
- return false;
- }
- if (left.sign && ! right.sign){
- return false;
- }
- if (! left.sign && right.sign){
- return true;
- }
- if (left.sign && right.sign){
- if (left.lenght < right.lenght){
- return true;
- }
- if (left.lenght > right.lenght){
- return false;
- }
- }
- if (! left.sign and ! right.sign){
- if (left.lenght < right.lenght){
- return false;
- }
- if (left.lenght > right.lenght){
- return true;
- }
- }
- for(int i = 0; i < left.lenght; ++i){
- if (left.data[i] < right.data[i]){
- if (left.sign){
- return true;
- }
- else{
- return false;
- }
- }
- if (left.data[i] == right.data[i]){
- continue;
- }
- if (left.data[i] > right.data[i]){
- if(left.sign){
- return false;
- }
- else {
- return true;
- }
- }
- }
- }
- bool operator> (const BigInt& left, const BigInt& right) {
- if (left == right){
- return false;
- }
- if (left < right){
- return false;
- }
- return true;
- }
- void difference (const int *x, const int *y, int *z, int length) {
- int* copy_of_x = new int[length];
- for (int i = 0; i < length; ++i){
- copy_of_x[i] = x[i];
- }
- for (int i = 0; i <= (length - 1); i++)
- {
- if (i < (length - 1))
- {
- --copy_of_x[i + 1];
- z[i] += 10 + copy_of_x[i];
- }
- else{
- z[i] += copy_of_x[i];
- }
- z[i] -= y[i];
- if (z[i] / 10 > 0){
- z[i + 1]++;
- z[i] %= 10;
- }
- }
- delete[] copy_of_x;
- }
- BigInt operator- (const BigInt& l, const BigInt& r) {
- BigInt left = l, right = r;
- BigInt result;
- if (left == right){
- result = 0;
- return result;
- }
- if (left.sign && right.sign) {
- if (left > right) {
- result.lenght = left.lenght;
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- difference(left.data, right.data, result.data, left.lenght);
- for (int i = left.lenght - 1; i >= 0; --i) {
- if (result.data[i] == 0) {
- --result.lenght;
- continue;
- } else {
- break;
- }
- }
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- reverse_array(result.data, result.lenght);
- result.sign = true;
- return result;
- }
- if (left < right) {
- result.lenght = right.lenght;
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- difference(right.data, left.data, result.data, right.lenght);
- for (int i = right.lenght - 1; i >= 0; --i) {
- if (result.data[i] == 0) {
- --result.lenght;
- continue;
- } else {
- break;
- }
- }
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- reverse_array(result.data, result.lenght);
- result.sign = false;
- return result;
- }
- }
- if (left.sign && !right.sign){
- BigInt temp = right;
- temp.sign = true;
- result = left + temp;
- return result;
- }
- if (!left.sign && right.sign){
- BigInt temp = left;
- temp.sign = true;
- result = temp + right;
- result.sign = false;
- return result;
- }
- if (!left.sign && !right.sign) {
- BigInt temp_right = left;
- BigInt temp_left = right;
- temp_left.sign = true;
- temp_right.sign = true;
- // result = right - left
- if (temp_left > temp_right){
- result.lenght = temp_left.lenght;
- reverse_array(temp_left.data, temp_left.lenght);
- reverse_array(temp_right.data, temp_right.lenght);
- difference(temp_left.data, temp_right.data, result.data, temp_left.lenght);
- for(int i = temp_left.lenght - 1; i >= 0; --i){
- if(result.data[i] == 0){
- --result.lenght;
- continue;
- }
- else{
- break;
- }
- }
- reverse_array(temp_left.data, temp_left.lenght);
- reverse_array(temp_right.data, temp_right.lenght);
- reverse_array(result.data, result.lenght);
- result.sign = true;
- return result;
- }
- if (temp_left < temp_right){
- result.lenght = temp_right.lenght;
- reverse_array(temp_left.data, temp_left.lenght);
- reverse_array(temp_right.data, temp_right.lenght);
- difference(temp_right.data, temp_left.data, result.data, temp_right.lenght);
- for(int i = temp_right.lenght - 1; i >= 0; --i){
- if(result.data[i] == 0){
- --result.lenght;
- continue;
- }
- else{
- break;
- }
- }
- reverse_array(temp_left.data, temp_left.lenght);
- reverse_array(temp_right.data, temp_right.lenght);
- reverse_array(result.data, result.lenght);
- result.sign = false;
- return result;
- }
- }
- }
- BigInt operator* (const BigInt& l, const BigInt& r){
- BigInt left = l, right = r;
- BigInt result;
- if (left == 0 || right == 0){
- result = 0;
- result.sign = true;
- return result;
- }
- result.lenght = left.lenght + right.lenght + 1;
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- for (int i = 0; i < left.lenght; ++i) {
- for (int j = 0; j < right.lenght; ++j) {
- result.data[i + j] += left.data[i] * right.data[j];
- }
- }
- for (int i = 0; i < result.lenght - 1; ++i) {
- result.data[i + 1] += result.data[i] / 10;
- result.data[i] %= 10;
- }
- while(result.data[result.lenght - 1] == 0){
- --result.lenght;
- }
- if (result.lenght == 0){
- ++result.lenght;
- }
- reverse_array(left.data, left.lenght);
- reverse_array(right.data, right.lenght);
- reverse_array(result.data, result.lenght);
- if (left.sign != right.sign){
- result.sign = false;
- }
- else{
- result.sign = true;
- }
- if (result.lenght > LENGTH_LIMIT){
- throw BigIntegerOverflow();
- }
- return result;
- }
- BigInt operator / (const BigInt& left, const BigInt& right){
- if (right == 1){
- return left;
- }
- if (left.lenght == right.lenght){
- BigInt result;
- int k = 1;
- while (right * k < left || right * k == left) {
- ++k;
- }
- --k;
- result = k;
- return result;
- }
- BigInt temp_left = left, temp_right = right;
- temp_left.sign = true;
- temp_right.sign = true;
- BigInt result;
- int counter = 0;
- if (temp_right.lenght > temp_left.lenght){
- result = 0;
- return result;
- }
- result.lenght = temp_left.lenght;
- BigInt temp;
- temp.lenght = temp_right.lenght;
- for(int i = 0; i < temp_right.lenght; ++i){
- temp.data[i] = temp_left.data[i];
- }
- int calls = 0;
- for (int i = temp_right.lenght; i < temp_left.lenght;) {
- while (temp < temp_right) {
- if (calls == 0) {
- if (temp.data[0] == 0) {
- temp.data[0] = temp_left.data[i];
- if (temp.data[0] == 0) {
- result.data[counter] = 0;
- ++counter;
- }
- ++calls;
- ++i;
- if (i == temp_left.lenght) {
- break;
- }
- } else {
- ++temp.lenght;
- temp.data[temp.lenght - 1] = temp_left.data[i];
- ++i;
- ++calls;
- if (i == temp_left.lenght) {
- break;
- }
- }
- }
- else {
- if (temp.data[0] == 0) {
- temp.data[0] = temp_left.data[i];
- if (temp.data[0] == 0) {
- result.data[counter] = 0;
- ++counter;
- }
- ++i;
- ++calls;
- if (i == temp_left.lenght) {
- break;
- }
- } else {
- ++temp.lenght;
- temp.data[temp.lenght - 1] = temp_left.data[i];
- ++i;
- ++calls;
- result.data[counter] = 0;
- ++counter;
- if (i == temp_left.lenght) {
- break;
- }
- }
- }
- }
- int k = 1;
- while (temp_right * k < temp || temp_right * k == temp) {
- ++k;
- }
- --k;
- if (k != 0) {
- temp = temp - temp_right * k;
- result.data[counter] = k;
- ++counter;
- calls = 0;
- }
- else{
- if (temp.data[0] != 0){
- result.data[counter] = 0;
- ++counter;
- }
- break;
- }
- }
- result.lenght = counter;
- return result;
- }
- BigInt operator % (const BigInt& left, const BigInt& right){
- BigInt temp;
- temp = left / right;
- return left - temp * right;
- }
- BigInt NOD(const BigInt& left, const BigInt& right){
- BigInt temp_right = right, temp_left = left;
- BigInt temp;
- if(temp_left > temp_right){
- while(true){
- if (temp_left % temp_right == 0){
- return temp_right;
- }
- temp = temp_right;
- temp_right = temp_left % temp_right;
- temp_left = temp;
- }
- }
- else{
- while(true){
- if (temp_right % temp_left == 0){
- return temp_left;
- }
- temp = temp_left;
- temp_left = temp_right % temp_left;
- temp_right = temp;
- }
- }
- }
- BigInt big_sqrt(const BigInt& big_int){
- BigInt result, temp;
- result.lenght = (big_int.lenght + 1) / 2;
- for(int i = 0; i < result.lenght; ++i){
- for(int j = 9; j >= 0; --j){
- result.data[i] = j;
- temp = result;
- if (result*temp < big_int || result*temp == big_int){
- break;
- }
- }
- }
- return result;
- }
- int main() {
- try {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- BigInt a, b;
- std::cin >> a >> b;
- int c;
- std::cin >> c;
- std::cout << (a * c == c * b) << '\n';
- std::cout << (a + 5 < b) << '\n';
- std::cout << (a <= b) << '\n';
- std::cout << (a > b - 5) << '\n';
- std::cout << (a >= b - 5) << '\n';
- std::cout << (a != b * c) << '\n';
- std::cout << (a == -b) << '\n';
- BigInt d("123"), e(1ULL << 63);
- std::cout << (d + c) * e << '\n';
- try {
- a += b;
- b = a - b;
- a -= b;
- std::cout << a + b << '\n';
- std::cout << a - b << '\n';
- std::cout << a * b << '\n';
- } catch (BigIntegerOverflow) {
- std::cout << "Overflow" << '\n';
- }
- } catch (const BigIntegerOverflow&){
- cout << "opana";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement