#include "num.h"
#include <iostream>

Num::Num(int value, int modulo) : value(value), modulo(modulo) {
  if (modulo != 0) {
    this->value = value % modulo;
  }
}

Num &Num::operator=(const Num &other) {
  this->value = other.value;
  this->modulo = other.modulo;
}

Num Num::operator+(const Num &other) {
  return {(this->value + other.value) %
      this->modulo, this->modulo};
}

Num Num::operator-(const Num &other) {
  return {(this->value - other.value) %
      this->modulo, this->modulo};
}
Num Num::operator*(const Num &other) {
  return {(this->value * other.value) %
      this->modulo, this->modulo};
}
Num Num::operator+(int num) {
  return {(this->value + num) %
      this->modulo, this->modulo};
}
Num Num::operator-(int num) {
  return {(this->value - num) %
      this->modulo, this->modulo};
}
Num Num::operator*(int num) {
  return {(this->value * num) %
      this->modulo, this->modulo};
}

Num &Num::operator+=(const Num &other) {
  this->value = (this->value + other.value) %
      this->modulo;
  return *this;
}

Num &Num::operator-=(const Num &other) {
  if (this->value < other.value) {
    this->value = this->modulo + this->value - other.value;

  } else {
    this->value = this->value - other.value % this->modulo;
  }
  return *this;
}

Num &Num::operator*=(const Num &other) {
  this->value = (this->value * other.value) %
      this->modulo;
  return *this;
}

Num &Num::operator+=(int num) {
  this->value = ((int64_t) this->value +
      (int64_t) num) % this->modulo;
  return *this;
}

Num &Num::operator-=(int num) {
  if (this->value < num) {
    this->value = this->modulo + this->value - num;

  } else {
    this->value = this->value - num % this->modulo;
  }
  return *this;
}

Num &Num::operator*=(int num) {
  this->value = ((int64_t) this->value *
      (int64_t) num) % this->modulo;
  return *this;
}