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

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

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

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

Num Num::operator+(int num) {
  int64_t answer = static_cast<int64_t>(value) + static_cast<int64_t>(num);
  if (modulo != 0)
    answer %= static_cast<int64_t>(modulo);
  return {static_cast<int>(answer), this->modulo};
}

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

Num Num::operator-(int num) {
  int64_t answer = static_cast<int64_t>(value) - static_cast<int64_t>(num);
  if (modulo != 0) {
    answer %= static_cast<int64_t>(modulo);
  }
  return {static_cast<int>(answer), this->modulo};
}

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

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

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

Num Num::operator+(const Num &other) {
  return *this + other.value;
}

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

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

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

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