#include <cmath> #include <cstdint> #include <iostream> #include <unordered_map> #include <vector> using namespace std; namespace { template <typename Scalar, typename Scalar2=Scalar> struct BasePoint { Scalar x; Scalar y; constexpr BasePoint(): x{0}, y{0} { } constexpr BasePoint(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BasePoint(BasePoint<OtherScalar, OtherScalar2> const& p): x(p.x), y(p.y) { } }; template <typename Scalar, typename Scalar2> constexpr bool operator==(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x == p2.x && p1.y == p2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x != p2.x || p1.y != p2.y; } template <typename Scalar, typename Scalar2=Scalar> struct BaseVector { Scalar x; Scalar y; constexpr BaseVector(): x{0}, y{0} { } constexpr BaseVector(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseVector(BaseVector<OtherScalar, OtherScalar2> const& v): x(v.x), y(v.y) { } int quadrant() const { if (x >= 0) { if (y >= 0) return 1; else return 4; } else { if (y >= 0) return 2; else return 3; } } }; template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x + b.x, a.y + b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v) { return v; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& v) { return {-v.x, -v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(BaseVector<Scalar, Scalar2> const& v, Scalar s) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(Scalar s, BaseVector<Scalar, Scalar2> const& v) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr Scalar2 operator*(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.x + Scalar2{a.y} * b.y; } template <typename Scalar, typename Scalar2> constexpr Scalar operator%(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.y - Scalar2{a.y} * b.x; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BasePoint<Scalar, Scalar2> const& a, BasePoint<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v, BasePoint<Scalar, Scalar2> const& p) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BasePoint<Scalar, Scalar2> const& p, BaseVector<Scalar, Scalar2> const& v) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr bool operator==(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x == v2.x && v1.y == v2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x != v2.x || v1.y != v2.y; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.quadrant() == v2.quadrant() && v1 % v2 == 0; } template <typename Scalar, typename Scalar2> ostream& operator<<(ostream& os, BaseVector<Scalar, Scalar2> const& v) { return os << '(' << v.x << ", " << v.y << ')'; } using Point = BasePoint<int, long long>; using Vector = BaseVector<int, long long>; using u32 = uint32_t; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } u32 count(int S, int s) { if (s >= S) return 0; return S - s; } u32 solve(int x, int y, int k) { u32 res = 0; vector<Vector> dirs; int s = 0; for (int dx = 1; dx <= k; ++dx) { for (int dy = 0; dy <= k; ++dy) { if (gcd(dx, dy) != 1) continue; int d2 = dx*dx + dy*dy; if (d2 > k*k) continue; int d = sqrt(d2) + .5; // XXX if (d*d != d2) continue; // clog << dx << ' ' << dy << endl; dirs.emplace_back(dx, dy); s += dx; } } s = min(s, max(x, y)); // clog << dirs.size() << endl; // clog << s << endl; vector<vector<u32>> counts(s + 1, vector<u32>(s + 1)); counts[0][0] = 1; for (auto const& d: dirs) { for (int dx = s - d.x; dx >= 0; --dx) { for (int dy = s - d.y; dy >= 0; --dy) { counts[dx + d.x][dy + d.y] += counts[dx][dy]; } } } vector<pair<Vector, u32>> tab; int cnt = 0; for (int dx = 0; dx <= s; ++dx) { for (int dy = 0; dy <= s; ++dy) { if (counts[dx][dy] != 0) { // clog << dx << " " << dy << " na " << counts[dx][dy] << " sposobów" << endl; tab.emplace_back(Vector{dx, dy}, counts[dx][dy]); ++cnt; } } } // clog << cnt << endl; vector<vector<unordered_map<int, u32>>> counts2(2*s + 1, vector<unordered_map<int, u32>>(2*s + 1)); for (auto const& t: tab) { for (auto const& u: tab) { Vector v = t.first + Vector{-u.first.y, u.first.x}; u32 w = t.first.x; if (w >= x || v.x >= x || v.y >= y) continue; // clog << v << " szerokości " << w << " na " << t.second * u.second << " sposobów" << endl; counts2[s + v.x][v.y][w] += t.second * u.second; } } for (int dx = 0; dx <= 2*s; ++dx) { for (int dy = 0; dy <= 2*s; ++dy) { u32 tmp = count(y, dy); if (tmp == 0) continue; auto const& cur = counts2[dx][dy]; if (cur.empty()) continue; for (auto const& a: cur) { for (auto const& b: cur) { u32 w = a.first - (dx - s - b.first); // XXX res += tmp * count(x, w) * a.second * b.second; // clog << "Wielokąt " << Vector{dx - s, dy} << " szerokości " << w << " razy " << a.second << "*" << b.second << "=" << a.second*b.second << endl; } } } } // clog << res << endl; res -= u32(x) * y; for (auto const& d: dirs) { res -= count(x, d.x) * count(y, d.y); res -= count(y, d.x) * count(x, d.y); } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int x, y, k; cin >> x >> y >> k; cout << solve(x, y, k) << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #include <cmath> #include <cstdint> #include <iostream> #include <unordered_map> #include <vector> using namespace std; namespace { template <typename Scalar, typename Scalar2=Scalar> struct BasePoint { Scalar x; Scalar y; constexpr BasePoint(): x{0}, y{0} { } constexpr BasePoint(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BasePoint(BasePoint<OtherScalar, OtherScalar2> const& p): x(p.x), y(p.y) { } }; template <typename Scalar, typename Scalar2> constexpr bool operator==(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x == p2.x && p1.y == p2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x != p2.x || p1.y != p2.y; } template <typename Scalar, typename Scalar2=Scalar> struct BaseVector { Scalar x; Scalar y; constexpr BaseVector(): x{0}, y{0} { } constexpr BaseVector(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseVector(BaseVector<OtherScalar, OtherScalar2> const& v): x(v.x), y(v.y) { } int quadrant() const { if (x >= 0) { if (y >= 0) return 1; else return 4; } else { if (y >= 0) return 2; else return 3; } } }; template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x + b.x, a.y + b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v) { return v; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& v) { return {-v.x, -v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(BaseVector<Scalar, Scalar2> const& v, Scalar s) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(Scalar s, BaseVector<Scalar, Scalar2> const& v) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr Scalar2 operator*(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.x + Scalar2{a.y} * b.y; } template <typename Scalar, typename Scalar2> constexpr Scalar operator%(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.y - Scalar2{a.y} * b.x; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BasePoint<Scalar, Scalar2> const& a, BasePoint<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v, BasePoint<Scalar, Scalar2> const& p) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BasePoint<Scalar, Scalar2> const& p, BaseVector<Scalar, Scalar2> const& v) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr bool operator==(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x == v2.x && v1.y == v2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x != v2.x || v1.y != v2.y; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.quadrant() == v2.quadrant() && v1 % v2 == 0; } template <typename Scalar, typename Scalar2> ostream& operator<<(ostream& os, BaseVector<Scalar, Scalar2> const& v) { return os << '(' << v.x << ", " << v.y << ')'; } using Point = BasePoint<int, long long>; using Vector = BaseVector<int, long long>; using u32 = uint32_t; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } u32 count(int S, int s) { if (s >= S) return 0; return S - s; } u32 solve(int x, int y, int k) { u32 res = 0; vector<Vector> dirs; int s = 0; for (int dx = 1; dx <= k; ++dx) { for (int dy = 0; dy <= k; ++dy) { if (gcd(dx, dy) != 1) continue; int d2 = dx*dx + dy*dy; if (d2 > k*k) continue; int d = sqrt(d2) + .5; // XXX if (d*d != d2) continue; // clog << dx << ' ' << dy << endl; dirs.emplace_back(dx, dy); s += dx; } } s = min(s, max(x, y)); // clog << dirs.size() << endl; // clog << s << endl; vector<vector<u32>> counts(s + 1, vector<u32>(s + 1)); counts[0][0] = 1; for (auto const& d: dirs) { for (int dx = s - d.x; dx >= 0; --dx) { for (int dy = s - d.y; dy >= 0; --dy) { counts[dx + d.x][dy + d.y] += counts[dx][dy]; } } } vector<pair<Vector, u32>> tab; int cnt = 0; for (int dx = 0; dx <= s; ++dx) { for (int dy = 0; dy <= s; ++dy) { if (counts[dx][dy] != 0) { // clog << dx << " " << dy << " na " << counts[dx][dy] << " sposobów" << endl; tab.emplace_back(Vector{dx, dy}, counts[dx][dy]); ++cnt; } } } // clog << cnt << endl; vector<vector<unordered_map<int, u32>>> counts2(2*s + 1, vector<unordered_map<int, u32>>(2*s + 1)); for (auto const& t: tab) { for (auto const& u: tab) { Vector v = t.first + Vector{-u.first.y, u.first.x}; u32 w = t.first.x; if (w >= x || v.x >= x || v.y >= y) continue; // clog << v << " szerokości " << w << " na " << t.second * u.second << " sposobów" << endl; counts2[s + v.x][v.y][w] += t.second * u.second; } } for (int dx = 0; dx <= 2*s; ++dx) { for (int dy = 0; dy <= 2*s; ++dy) { u32 tmp = count(y, dy); if (tmp == 0) continue; auto const& cur = counts2[dx][dy]; if (cur.empty()) continue; for (auto const& a: cur) { for (auto const& b: cur) { u32 w = a.first - (dx - s - b.first); // XXX res += tmp * count(x, w) * a.second * b.second; // clog << "Wielokąt " << Vector{dx - s, dy} << " szerokości " << w << " razy " << a.second << "*" << b.second << "=" << a.second*b.second << endl; } } } } // clog << res << endl; res -= u32(x) * y; for (auto const& d: dirs) { res -= count(x, d.x) * count(y, d.y); res -= count(y, d.x) * count(x, d.y); } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int x, y, k; cin >> x >> y >> k; cout << solve(x, y, k) << endl; return 0; } |