#include "num.h"

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

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

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

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

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

Num Num::operator+(int num) {
  Num res = Num(this->value + num, this->modulo);
  return res;
}

Num Num::operator-(int num) {
  Num res = Num(this->value - num, this->modulo);
  return res;
}

Num Num::operator*(int num) {
  Num res = Num(this->value * num, this->modulo);
  return res;
}

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

Num& Num::operator-=(const Num& other) {
  int res = this->value - other.value;
  if (this->modulo != 0)
    res %= this->modulo;
  if (res < 0)
    res = this->modulo + res;
  this->value = res;
  return *this;
}

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

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

Num& Num::operator-=(int num) {
  int res = this->value - num;
  if (this->modulo != 0)
    res %= this->modulo;
  if (res < 0)
    res = this->modulo + res;
  this->value = res;
  return *this;
}

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