#include #include "num.h" // Mod that supports negative numbers int64_t mod(int64_t 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 { int64_t sum = static_cast (other.value) + value; return {static_cast((modulo == 0 ? sum : sum % modulo)), this->modulo}; } Num Num::operator*(const Num &other) const { int64_t sum = static_cast(other.value) * value; return {static_cast (modulo == 0 ? sum : sum % modulo), this->modulo}; } Num Num::operator-(int num) const { int64_t sum = static_cast(value) - num; return {static_cast (modulo == 0 ? sum : mod(sum, modulo)), modulo}; } Num Num::operator-(const Num &other) const { int64_t sum = static_cast(value) - other.value; return {static_cast (modulo == 0 ? sum : mod(sum, modulo)), this->modulo}; } Num Num::operator+(int num) const { int64_t sum = static_cast(value) + num; return {static_cast(modulo == 0 ? sum : sum % modulo), modulo}; } Num Num::operator*(int num) const { int64_t sum = static_cast(num) * value; return {static_cast (modulo == 0 ? sum : sum % modulo), this->modulo}; } Num &Num::operator+=(const Num &other) { int64_t sum = static_cast(other.value) + value; this->value = static_cast (modulo == 0 ? sum : sum % modulo); return *this; } Num &Num::operator-=(const Num &other) { int64_t sum = static_cast(value) - other.value; this->value = static_cast (modulo == 0 ? sum : mod(sum, modulo)); return *this; } Num &Num::operator+=(int num) { int64_t sum = static_cast(num) + value; this->value = static_cast (modulo == 0 ? sum : sum % modulo); return *this; } Num &Num::operator*=(const Num &other) { int64_t sum = static_cast( other.value) * value; this->value = static_cast (modulo == 0 ? sum : sum % modulo); return *this; } Num &Num::operator-=(int num) { int64_t sum = static_cast(value) - num; this->value = static_cast (modulo == 0 ? sum : mod(sum, modulo)); return *this; } Num &Num::operator*=(int num) { int64_t sum = static_cast(num) * value; this->value = static_cast (modulo == 0 ? sum : sum % modulo); return *this; }