#include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, map<L,R> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const LD kEps = 1e-9; const LD kPi = 2 * acos(0); int Sq(int x) { return x * x; } struct Point { int x, y; Point() {} Point(int a, int b) : x(a), y(b) {} Point(const Point& a) : x(a.x), y(a.y) {} void operator=(const Point& a) { x = a.x; y = a.y; } Point operator+(const Point& a) const { Point p(x + a.x, y + a.y); return p; } Point operator-(const Point& a) const { Point p(x - a.x, y - a.y); return p; } Point operator*(int a) const { Point p(x * a, y * a); return p; } Point operator/(int a) const { assert(a != 0); Point p(x / a, y / a); return p; } Point& operator+=(const Point& a) { x += a.x; y += a.y; return *this; } Point& operator-=(const Point& a) { x -= a.x; y -= a.y; return *this; } Point& operator*=(int a) { x *= a; y *= a; return *this;} Point& operator/=(int a) { assert(a != 0); x /= a; y /= a; return *this; } bool IsZero() const { return x == 0 && y == 0; } bool operator==(const Point& a) const { return (*this - a).IsZero(); } bool operator!=(const Point& a) const { return !(*this - a).IsZero(); } int CrossProd(const Point& a) const { return x * a.y - y * a.x; } int CrossProd(Point a, Point b) const { a -= *this; b -= *this; return a.CrossProd(b); } int DotProd(const Point& a) const { return x * a.x + y * a.y; } LD Angle() const { return atan2(y, x); } static bool LexCmp(const Point& a, const Point& b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } int SqNorm() { return x * x + y * y; } friend ostream& operator<<(ostream& out, Point m); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } int is_sq(int a){ int tmp = sqrt(a); return tmp * tmp == a; } int sq(int a){ return a * a; } int dist(int x, int y){ return x * x + y * y; } vector<Point> get_sides(int maxLen){ vector<Point> res; FOR(i, 1, maxLen){ REP(j, i){ if(__gcd(i, j) != 1) continue; if(!is_sq(dist(i, j))) continue; if(dist(i, j) > maxLen * maxLen) break; debug(i, j); for(int a : {-i, i}){ for(int b : {-j, j}){ res.push_back(Point(a,b)); res.push_back(Point(b,a)); } } } } sort(ALL(res), Point::LexCmp); res.resize(unique(ALL(res)) - res.begin()); return res; } map<PII, uint32_t> mapka; Point NONE(-1,-1); vector<Point> sides; vector<Point> acc; int maxSides = 0; void bt(Point cur, Point prev = NONE, int maxx = 0, int minx = 0, int maxy = 0){ maxi(maxx, cur.x); mini(minx, cur.x); maxi(maxy, cur.y); if(cur.IsZero() && prev != NONE){ maxi(maxSides, SZ(acc)); mapka[{maxx - minx, maxy}]++; return; } for(auto s: sides){ s += cur; if(s.y < 0){ continue; } if(prev == NONE){ if(s.y == 0) continue; goto nxt; } if(s == Point(0,0) && prev != Point(0,0)){ goto nxt; } { int cpd1= prev.CrossProd(cur, s); int cpd2 = cur.CrossProd(s); if(cpd1 <= 0 || cpd2 <= 0){ continue; } } nxt:; acc.PB(cur); bt(s, cur, maxx, minx, maxy); acc.pop_back(); } } int32_t main(int32_t, char ** argv) { ios_base::sync_with_stdio(0); cin.tie(0); int x, y, k; cin>>x>>y>>k; sides = get_sides(k); debug(sides.size()); uint32_t res = 0; if(k <= 15){ bt(Point(0,0)); debug(SZ(mapka)); for(auto rec: mapka){ uint32_t tx = max(0LL, x- rec.st.st); uint32_t ty = max(0LL, y- rec.st.nd); res += tx * ty * rec.nd; } } else{ bt(Point(0,0)); cout<<mapka<<endl; } cout<<res<<endl; }
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 | #include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, map<L,R> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const LD kEps = 1e-9; const LD kPi = 2 * acos(0); int Sq(int x) { return x * x; } struct Point { int x, y; Point() {} Point(int a, int b) : x(a), y(b) {} Point(const Point& a) : x(a.x), y(a.y) {} void operator=(const Point& a) { x = a.x; y = a.y; } Point operator+(const Point& a) const { Point p(x + a.x, y + a.y); return p; } Point operator-(const Point& a) const { Point p(x - a.x, y - a.y); return p; } Point operator*(int a) const { Point p(x * a, y * a); return p; } Point operator/(int a) const { assert(a != 0); Point p(x / a, y / a); return p; } Point& operator+=(const Point& a) { x += a.x; y += a.y; return *this; } Point& operator-=(const Point& a) { x -= a.x; y -= a.y; return *this; } Point& operator*=(int a) { x *= a; y *= a; return *this;} Point& operator/=(int a) { assert(a != 0); x /= a; y /= a; return *this; } bool IsZero() const { return x == 0 && y == 0; } bool operator==(const Point& a) const { return (*this - a).IsZero(); } bool operator!=(const Point& a) const { return !(*this - a).IsZero(); } int CrossProd(const Point& a) const { return x * a.y - y * a.x; } int CrossProd(Point a, Point b) const { a -= *this; b -= *this; return a.CrossProd(b); } int DotProd(const Point& a) const { return x * a.x + y * a.y; } LD Angle() const { return atan2(y, x); } static bool LexCmp(const Point& a, const Point& b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } int SqNorm() { return x * x + y * y; } friend ostream& operator<<(ostream& out, Point m); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } int is_sq(int a){ int tmp = sqrt(a); return tmp * tmp == a; } int sq(int a){ return a * a; } int dist(int x, int y){ return x * x + y * y; } vector<Point> get_sides(int maxLen){ vector<Point> res; FOR(i, 1, maxLen){ REP(j, i){ if(__gcd(i, j) != 1) continue; if(!is_sq(dist(i, j))) continue; if(dist(i, j) > maxLen * maxLen) break; debug(i, j); for(int a : {-i, i}){ for(int b : {-j, j}){ res.push_back(Point(a,b)); res.push_back(Point(b,a)); } } } } sort(ALL(res), Point::LexCmp); res.resize(unique(ALL(res)) - res.begin()); return res; } map<PII, uint32_t> mapka; Point NONE(-1,-1); vector<Point> sides; vector<Point> acc; int maxSides = 0; void bt(Point cur, Point prev = NONE, int maxx = 0, int minx = 0, int maxy = 0){ maxi(maxx, cur.x); mini(minx, cur.x); maxi(maxy, cur.y); if(cur.IsZero() && prev != NONE){ maxi(maxSides, SZ(acc)); mapka[{maxx - minx, maxy}]++; return; } for(auto s: sides){ s += cur; if(s.y < 0){ continue; } if(prev == NONE){ if(s.y == 0) continue; goto nxt; } if(s == Point(0,0) && prev != Point(0,0)){ goto nxt; } { int cpd1= prev.CrossProd(cur, s); int cpd2 = cur.CrossProd(s); if(cpd1 <= 0 || cpd2 <= 0){ continue; } } nxt:; acc.PB(cur); bt(s, cur, maxx, minx, maxy); acc.pop_back(); } } int32_t main(int32_t, char ** argv) { ios_base::sync_with_stdio(0); cin.tie(0); int x, y, k; cin>>x>>y>>k; sides = get_sides(k); debug(sides.size()); uint32_t res = 0; if(k <= 15){ bt(Point(0,0)); debug(SZ(mapka)); for(auto rec: mapka){ uint32_t tx = max(0LL, x- rec.st.st); uint32_t ty = max(0LL, y- rec.st.nd); res += tx * ty * rec.nd; } } else{ bt(Point(0,0)); cout<<mapka<<endl; } cout<<res<<endl; } |