#include "num.h"

// Mod that supports negative numbers
long long mod(long long x, int m) {
  return (x % m + m) % m;
}

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

Num &Num::operator=(const Num &other) {
  this->value = other.value;
  this->value = modulo == 0 ? value : value % modulo;
  return *this;
}
Num Num::operator+(const Num &other) const {
  long long sum = static_cast<long long> (other.value) + value;
  return {static_cast<int>((modulo == 0 ? sum : sum % modulo)), this->modulo};
}
Num Num::operator*(const Num &other) const {
  long long sum = static_cast<long long>(other.value) * value;
  return {static_cast<int> (modulo == 0 ? sum : sum % modulo), this->modulo};
}
Num Num::operator-(int num) const {
  long long sum = static_cast<long long>(value) - num;
  return {static_cast<int> (modulo == 0 ? sum : mod(sum, modulo)), modulo};
}
Num Num::operator-(const Num &other) const {
  long long sum = static_cast<long long>(value) - other.value;
  return {static_cast<int> (modulo == 0 ? sum : mod(sum, modulo)), this->modulo};
}
Num Num::operator+(int num) const {
  long long sum = static_cast<long long>(value) + num;
  return {static_cast<int>(modulo == 0 ? sum : sum % modulo), modulo};
}
Num Num::operator*(int num) const {
  long long sum = static_cast<long long>(num) * value;
  return {static_cast<int> (modulo == 0 ? sum : sum % modulo), this->modulo};
}
Num &Num::operator+=(const Num &other) {
  long long sum = static_cast<long long>( other.value) + value;
  this->value = static_cast<int> (modulo == 0 ? sum : sum % modulo);
  return *this;
}
Num &Num::operator-=(const Num &other) {
  long long sum = static_cast<long long>(value) - other.value;
  this->value = static_cast<int> (modulo == 0 ? sum : mod(sum, modulo));
  return *this;
}
Num &Num::operator+=(int num) {
  long long sum = static_cast<long long>( num) + value;
  this->value = static_cast<int> (modulo == 0 ? sum : sum % modulo);
  return *this;
}
Num &Num::operator*=(const Num &other) {
  long long sum = static_cast<long long>( other.value) * value;
  this->value = static_cast<int> (modulo == 0 ? sum : sum % modulo);
  return *this;
}
Num &Num::operator-=(int num) {
  long long sum = static_cast<long long>( value) - num;
  this->value = static_cast<int> (modulo == 0 ? sum : mod(sum, modulo));
  return *this;
}
Num &Num::operator*=(int num) {
  long long sum = static_cast<long long>( num) * value;
  this->value = static_cast<int> (modulo == 0 ? sum : sum % modulo);
  return *this;
}