#if __INCLUDE_LEVEL__ == 0 #include __BASE_FILE__ using Mint = atcoder::modint; Mint C2(int n) { return n * int64_t(n - 1) / 2; } void Solve() { int X, Y, M; IN(X, Y, M); Mint::set_mod(M); Mint ans = 0; for (int _ : Rep(0, 2)) { ans += (C2(DivFloor(X, 2)) + C2(DivCeil(X, 2))) * Y; vector> v; for (int m : Rep(1, 1700)) { for (int n : Rep(1, m)) { int a = m * m - n * n; int b = 2 * m * n; int c = m * m + n * n; if (a > b) { swap(a, b); } if (gcd(gcd(a, b), c) == 1 && 2 * a < X && 2 * b < Y) { v.emplace_back(a, b); } } } ranges::sort(v); APPLY(v.erase, ranges::unique(v)); for (auto [a, b] : v) { for (int A = a, B = b; 2 * A < X && 2 * B < Y; A += a, B += b) { ans += Mint(X - 2 * A) * Mint(Y - 2 * B); } } swap(X, Y); } OUT(2 * ans); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solve(); } #elif __INCLUDE_LEVEL__ == 1 #include #include template concept Range = std::ranges::range && !std::convertible_to; template concept Tuple = std::__is_tuple_like::value && !Range; namespace std { istream& operator>>(istream& is, Range auto&& r) { for (auto&& e : r) is >> e; return is; } istream& operator>>(istream& is, Tuple auto&& t) { apply([&](auto&... xs) { (is >> ... >> xs); }, t); return is; } ostream& operator<<(ostream& os, Range auto&& r) { auto sep = ""; for (auto&& e : r) os << exchange(sep, " ") << e; return os; } ostream& operator<<(ostream& os, Tuple auto&& t) { auto sep = ""; apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t); return os; } template * = nullptr> istream& operator>>(istream& is, T& x) { int v; is >> v; x = T::raw(v); return is; } template * = nullptr> ostream& operator<<(ostream& os, const T& x) { return os << x.val(); } } // namespace std using namespace std; #define _ _ [[maybe_unused]] #define LAMBDA(x, ...) [&](auto&& x) -> decltype(auto) { return __VA_ARGS__; } #define LAMBDA2(x, y, ...) [&](auto&& x, auto&& y) -> decltype(auto) { return __VA_ARGS__; } #define ALL(r) begin(r), end(r) #define APPLY(f, r, ...) LAMBDA(_r, f(ALL(_r), ##__VA_ARGS__))(r) #define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__) #define DivFloor(...) LAMBDA2(x, y, x / y - ((x ^ y) < 0 && x % y != 0))(__VA_ARGS__) #define DivCeil(...) LAMBDA2(x, y, x / y + ((x ^ y) >= 0 && x % y != 0))(__VA_ARGS__) #define IN(...) cin >> forward_as_tuple(__VA_ARGS__) #define OUT(...) cout << forward_as_tuple(__VA_ARGS__) << '\n' #endif // __INCLUDE_LEVEL__ == 1