#include "num.h"

Num::Num(int value, int modulo):
  value((modulo == 0) ? value : value % modulo),
  modulo(modulo) {}

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

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

Num& Num::operator+=(const Num& other) {
  int res = this->value + other.value;
  this->value = (this->modulo == 0) ? res : res % this->modulo;
}
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;
}
Num& Num::operator*=(const Num& other) {
  long res = (long)this->value * (long)other.value;
  this->value = (this->modulo == 0) ? res : res % this->modulo;
}
Num& Num::operator+=(int num) {
  long res = (long)this->value + num;
  this->value = (this->modulo == 0) ? res : res % this->modulo;
}
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;
}
Num& Num::operator*=(int num) {
  long res = (long)this->value * num;
  this->value = (this->modulo == 0) ? res : res % this->modulo;
}