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