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

Num::Num(int value, int modulo) : 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) {
    this->modulo = other.modulo;
    this->value = (this->value + other.value) % this->modulo;
    return *this;
}

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

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

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

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

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