QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#562988 | #8669. 正方形计数 | sha7dow | 100 ✓ | 3818ms | 3860kb | C++14 | 10.1kb | 2024-09-14 00:00:04 | 2024-09-14 00:00:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using db = double;
using ldb = long double;
#define _T template <class T>
#define _FT template<class T, class FT = typename common_type<T, double>::type>
#define _F(X, Y) using X = Y<T, FT>; using F##X = Y<FT>
_T constexpr T eps = 0;
template<> constexpr double eps<double> = 1e-9;
template<> constexpr long double eps<long double> = 1e-11;
_T int sign(T x) {
return (x > eps<T>) - (x < -eps<T>);
}
_T int cmp(T x, T y) {
return sign(x - y);
}
_FT struct Point {
_F(P, Point);
T x, y;
Point() = default;
Point(T x, T y) : x(x), y(y) {}
template <class U, class FU>
explicit Point(const Point<U, FU>& p) : x(p.x), y(p.y) {}
T det(const P& p) const { return x * p.y - y * p.x; }
T dot(const P& p) const { return x * p.x + y * p.y; }
P operator+(const P& p) const { return {x + p.x, y + p.y}; }
P operator-(const P& p) const { return {x - p.x, y - p.y}; }
P operator*(T d) const { return {x * d, y * d}; }
P operator-() const { return {-x, -y}; }
FP operator/(FT d) const { return {x / d, y / d}; }
int quad() const {
return sign(y) ? sign(y) + (sign(y) > 0) : sign(x) - (sign(x) < 0);
}
struct paCmp {
bool operator()(const P& p, const P& q) const {
int x = p.quad(), y = q.quad();
if (x != y) return x < y;
const auto dt = p.det(q);
return sign(dt) > 0;
}
};
friend istream& operator>>(istream& is, P& p) {
return is >> p.x >> p.y;
}
friend ostream& operator<<(ostream& os, const P& p) {
return os << p.x << ' ' << p.y;
}
};
_FT struct Line {
_F(L, Line);
_F(P, Point);
P u, v;
Line() = default;
Line(P u, P v) : u(u), v(v) {}
template <class U, class FU>
explicit Line(const Line<U, FU>& l) : u(l.u), v(l.v) {}
T cross(const P& p) const { // uv.det(up)
return (v.x - u.x) * (p.y - u.y) - (p.x - u.x) * (v.y - u.y);
}
int toLeft(const P& p) const { return sign(cross(p)); }
FP getInter(const L& l) const {
T c1 = l.cross(u), c2 = -l.cross(v);
return (FP(u) * c2 + FP(v) * c1) / (c1 + c2);
}
struct paCmp {
bool operator()(const L& l1, const L& l2) const {
P d1 = l1.v - l1.u, d2 = l2.v - l2.u;
if (sign(d1.det(d2)) == 0 && sign(d1.dot(d2)) >= 0)
return l1.toLeft(l2.u) < 0;
return typename P::paCmp()(d1, d2);
}
};
};
ll floor(ll x, ll y) {
return x >= 0 ? x / y : (x + 1) / y - 1;
}
ll ceil(ll x, ll y) {
return x <= 0 ? x / y : (x - 1) / y + 1;
}
_T ll euclid(T a, T b, T c, ll n) {
if (n < 0) return -euclid(-a, b - a, c, -n);
T p = floor(a, c), q = floor(b, c);
if (p || q) {
return n * (n - 1) / 2 * p + n * q +
euclid(a - p * c, b - q * c, c, n);
}
ll m = a * n + b;
return m < c ? 0 : euclid(c, T(m % c), a, m / c);
}
_T ll countHalfplaneInter(const vector<Line<T>>& l) {
if (l.empty()) return 0;
ll s = 0;
vector<T> a, b, c;
a.reserve(l.size());
b.reserve(l.size());
c.reserve(l.size());
for (auto [u, v] : l) {
a.emplace_back(v.y - u.y);
b.emplace_back(u.x - v.x);
c.emplace_back(u.x * -a.back() + u.y * -b.back());
}
int k = l.size() - 1;
ll ux, uy, un,
vx = 1ll * b[k] * c[0] - 1ll * b[0] * c[k],
vy = 1ll * c[k] * a[0] - 1ll * c[0] * a[k],
vn = a[k] * b[0] - a[0] * b[k];
for (int i = 0; i < l.size(); i++) {
int u = i - 1 >= 0 ? i - 1 : l.size() - 1,
v = i + 1 < l.size() ? i + 1 : 0;
ux = vx, uy = vy, un = vn,
vx = 1ll * b[i] * c[v] - 1ll * b[v] * c[i],
vy = 1ll * c[i] * a[v] - 1ll * c[v] * a[i],
vn = a[i] * b[v] - a[v] * b[i];
if (b[i] < 0)
s -= euclid(a[i], c[i] - 1, -b[i], floor(vx, vn) + 1) -
euclid(a[i], c[i] - 1, -b[i], floor(ux - 1, un) + 1);
if (b[i] > 0)
s += euclid(-a[i], -c[i], b[i], floor(ux, un) + 1) -
euclid(-a[i], -c[i], b[i], floor(vx - 1, vn) + 1);
if (b[i] < 0 && b[u] < 0 && ux % un == 0)
s += floor(uy - 1, un);
if (b[i] > 0 && b[v] > 0 && vx % vn == 0)
s -= floor(vy, vn);
}
return s;
}
using P = Point<int>;
using L = Line<int>;
int cnt;
array<int, 2002> vis, prime, val;
void solve() {
int n;
cin >> n;
vector<P> p(n);
int V = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
V = max({V, p[i].x, p[i].y});
}
reverse(p.begin(), p.end());
vis[1] = 1;
for (int i = 2; i <= V; i++) {
if (!vis[i]) prime[++cnt] = i;
for (int j = 1; j <= cnt && i * prime[j] <= V; j++)
if (vis[i * prime[j]] = 0, i % prime[j] ==0) break;
}
vector<L> lll, q(100);
for (int i = 0; i < n; i++) {
lll.push_back({p[i], p[(i + 1) % n]});
}
sort(lll.begin(), lll.end(), L::paCmp());
ll ans = 0;
for (int xx = 1; xx <= V; xx++)
{
cnt = 0, vis.fill(0);
vis[1]=1, val[1] = 1;
for (int i = 2; i <= V; i++) {
if (!vis[i]) val[i] = __gcd(xx, i);
for (int j = 1; j <= cnt && i * prime[j] <= V; j++)
{
vis[i * prime[j]] = 1;
if (i % prime[j] ==0) break;
val[i * prime[j]] = val[i] * val[prime[j]];
}
}
for (int yy = 1; yy <= V; yy++)
if (val[yy] == 1)
for (int x = xx, y = yy; x <= V && y <= V; x += xx, y += yy) {
auto l = lll;
auto halfplane = [&](vector<L>& l) {
auto check = [](const L& l1, const L& l2, const L& l3) {
return Line<db>(Point<db>(l1.u), Point<db>(l1.v)).toLeft(l2.getInter(l3)) < 0;
};
vector<L> nw, nl;
auto mer = [&](P d) {
nl.clear();
for (int i = 0; i < n; i++) {
nl.push_back({lll[i].u - d, lll[i].v - d});
}
nw.resize(n + l.size());
merge(l.begin(), l.end(), nl.begin(), nl.end(), nw.begin(), L::paCmp());
swap(l, nw);
};
mer({x, y});
mer({-y, x});
mer({x - y, x + y});
// cout << l.size() << endl;
int st = 0, ed = -1;
for (int i = 0; i < l.size(); i++) {
P p = l[i].v - l[i].u;
if (i > 0 && (l[i - 1].v - l[i - 1].u).det(p) == 0 &&
sign((l[i - 1].v - l[i - 1].u).dot(p)) > 0)
continue;
while (ed > st && check(l[i], q[ed], q[ed - 1]))
ed--;
while (ed > st && check(l[i], q[st], q[st + 1]))
st++;
if (ed >= st && (q[ed].v - q[ed].u).det(p) <= 0)
return vector<L>{};
q[++ed] = l[i];
}
while (ed > st && check(q[st], q[ed], q[ed - 1]))
ed--;
while (ed > st && check(q[ed], q[st], q[st + 1]))
st++;
return vector<L>(q.begin() + st, q.begin() + ed + 1);
};
l = halfplane(l);
ll cnt = countHalfplaneInter(l);
if (!cnt) break;
ans += cnt;
}
}
for (int x = 1, y = 0; x <= V; x++) {
auto l = lll;
auto halfplane = [&](vector<L>& l) {
auto check = [](const L& l1, const L& l2, const L& l3) {
return Line<db>(Point<db>(l1.u), Point<db>(l1.v)).toLeft(l2.getInter(l3)) < 0;
};
vector<L> nw, nl;
auto mer = [&](P d) {
nl.clear();
for (int i = 0; i < n; i++) {
nl.push_back({lll[i].u - d, lll[i].v - d});
}
nw.resize(n + l.size());
merge(l.begin(), l.end(), nl.begin(), nl.end(), nw.begin(), L::paCmp());
swap(l, nw);
};
mer({x, y});
mer({-y, x});
mer({x - y, x + y});
// cout << l.size() << endl;
int st = 0, ed = -1;
for (int i = 0; i < l.size(); i++) {
P p = l[i].v - l[i].u;
if (i > 0 && (l[i - 1].v - l[i - 1].u).det(p) == 0 &&
sign((l[i - 1].v - l[i - 1].u).dot(p)) > 0)
continue;
while (ed > st && check(l[i], q[ed], q[ed - 1]))
ed--;
while (ed > st && check(l[i], q[st], q[st + 1]))
st++;
if (ed >= st && (q[ed].v - q[ed].u).det(p) <= 0)
return vector<L>{};
q[++ed] = l[i];
}
while (ed > st && check(q[st], q[ed], q[ed - 1]))
ed--;
while (ed > st && check(q[ed], q[st], q[st + 1]))
st++;
return vector<L>(q.begin() + st, q.begin() + ed + 1);
};
l = halfplane(l);
ll cnt = countHalfplaneInter(l);
if (!cnt) break;
ans += cnt;
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1178ms
memory: 3624kb
input:
4 131 603 131 1828 1919 1828 1919 603
output:
361182910200
result:
ok 1 number(s): "361182910200"
Test #2:
score: 10
Accepted
time: 207ms
memory: 3624kb
input:
4 239 211 239 962 261 962 261 211
output:
1498772
result:
ok 1 number(s): "1498772"
Test #3:
score: 10
Accepted
time: 1656ms
memory: 3564kb
input:
4 0 0 0 2000 2000 2000 2000 0
output:
1336001667000
result:
ok 1 number(s): "1336001667000"
Test #4:
score: 10
Accepted
time: 136ms
memory: 3856kb
input:
4 36 771 36 786 672 786 672 771
output:
427720
result:
ok 1 number(s): "427720"
Test #5:
score: 10
Accepted
time: 15ms
memory: 3620kb
input:
4 0 100 100 200 200 100 100 0
output:
34001650
result:
ok 1 number(s): "34001650"
Subtask #2:
score: 25
Accepted
Test #6:
score: 25
Accepted
time: 948ms
memory: 3640kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 25
Accepted
time: 177ms
memory: 3656kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: 25
Accepted
time: 1096ms
memory: 3736kb
input:
3 0 0 0 2000 2000 0
output:
222889277611
result:
ok 1 number(s): "222889277611"
Test #9:
score: 25
Accepted
time: 119ms
memory: 3776kb
input:
3 36 771 36 786 672 771
output:
98847
result:
ok 1 number(s): "98847"
Test #10:
score: 25
Accepted
time: 3ms
memory: 3552kb
input:
3 0 0 0 100 100 0
output:
1473186
result:
ok 1 number(s): "1473186"
Subtask #3:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 1ms
memory: 3636kb
input:
8 0 13 4 15 15 15 15 6 13 1 12 0 5 0 0 6
output:
4047
result:
ok 1 number(s): "4047"
Test #12:
score: 15
Accepted
time: 1ms
memory: 3844kb
input:
8 0 4 1 15 2 15 15 14 15 4 14 0 1 0 0 2
output:
4200
result:
ok 1 number(s): "4200"
Test #13:
score: 15
Accepted
time: 0ms
memory: 3644kb
input:
5 7 15 15 13 15 0 3 0 0 15
output:
3635
result:
ok 1 number(s): "3635"
Test #14:
score: 15
Accepted
time: 1ms
memory: 3784kb
input:
8 0 12 2 14 7 15 13 15 15 10 15 1 8 0 0 0
output:
4511
result:
ok 1 number(s): "4511"
Test #15:
score: 15
Accepted
time: 0ms
memory: 3624kb
input:
6 0 11 3 15 7 15 15 12 10 0 0 0
output:
3006
result:
ok 1 number(s): "3006"
Test #16:
score: 15
Accepted
time: 0ms
memory: 3608kb
input:
5 0 0 0 2 1 2 2 1 2 0
output:
4
result:
ok 1 number(s): "4"
Subtask #4:
score: 20
Accepted
Dependency #3:
100%
Accepted
Test #17:
score: 20
Accepted
time: 74ms
memory: 3640kb
input:
8 49 299 144 300 300 260 250 15 115 0 30 0 23 19 0 85
output:
443602646
result:
ok 1 number(s): "443602646"
Test #18:
score: 20
Accepted
time: 76ms
memory: 3856kb
input:
8 0 133 103 300 130 300 257 294 297 227 300 150 277 40 161 4
output:
351466521
result:
ok 1 number(s): "351466521"
Test #19:
score: 20
Accepted
time: 76ms
memory: 3572kb
input:
8 76 286 114 300 300 300 300 205 291 0 47 0 4 57 2 235
output:
605026927
result:
ok 1 number(s): "605026927"
Test #20:
score: 20
Accepted
time: 70ms
memory: 3644kb
input:
8 0 102 40 274 282 300 300 234 267 0 34 0 6 57 0 86
output:
497330741
result:
ok 1 number(s): "497330741"
Test #21:
score: 20
Accepted
time: 67ms
memory: 3584kb
input:
7 0 288 156 300 212 300 265 176 300 86 278 0 0 36
output:
446722651
result:
ok 1 number(s): "446722651"
Subtask #5:
score: 15
Accepted
Dependency #4:
100%
Accepted
Test #22:
score: 15
Accepted
time: 371ms
memory: 3852kb
input:
5 257 800 766 800 800 353 667 0 42 0
output:
18881369614
result:
ok 1 number(s): "18881369614"
Test #23:
score: 15
Accepted
time: 432ms
memory: 3624kb
input:
8 691 800 737 795 800 651 372 98 136 266 118 318 24 629 12 753
output:
8760058886
result:
ok 1 number(s): "8760058886"
Test #24:
score: 15
Accepted
time: 309ms
memory: 3784kb
input:
8 718 800 740 800 800 726 800 670 711 367 595 150 86 0 57 136
output:
3064355626
result:
ok 1 number(s): "3064355626"
Test #25:
score: 15
Accepted
time: 526ms
memory: 3628kb
input:
8 0 347 16 449 364 798 674 800 750 800 797 14 195 0 0 70
output:
23587042437
result:
ok 1 number(s): "23587042437"
Test #26:
score: 15
Accepted
time: 583ms
memory: 3844kb
input:
8 322 800 596 800 686 777 800 280 764 69 396 0 46 179 0 660
output:
23185884331
result:
ok 1 number(s): "23185884331"
Subtask #6:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 15
Accepted
time: 3495ms
memory: 3652kb
input:
8 0 1150 314 2000 1101 2000 1617 1607 1778 551 738 0 607 10 0 1011
output:
577130875850
result:
ok 1 number(s): "577130875850"
Test #28:
score: 15
Accepted
time: 3590ms
memory: 3644kb
input:
8 0 1841 1526 2000 1981 1680 1968 678 1893 26 973 0 616 315 524 434
output:
735496008519
result:
ok 1 number(s): "735496008519"
Test #29:
score: 15
Accepted
time: 3005ms
memory: 3820kb
input:
6 0 258 10 2000 1730 2000 2000 1510 1973 0 0 129
output:
1203935109430
result:
ok 1 number(s): "1203935109430"
Test #30:
score: 15
Accepted
time: 3110ms
memory: 3568kb
input:
7 200 2000 1686 2000 1951 1878 2000 863 1422 0 21 0 0 1015
output:
1100462975231
result:
ok 1 number(s): "1100462975231"
Test #31:
score: 15
Accepted
time: 3818ms
memory: 3628kb
input:
8 701 2000 1449 2000 1847 1928 2000 1496 1987 668 1588 108 263 0 0 1985
output:
997591862206
result:
ok 1 number(s): "997591862206"
Test #32:
score: 15
Accepted
time: 3514ms
memory: 3860kb
input:
8 15 2000 1235 2000 1545 1886 1970 1526 1828 427 1238 97 372 0 0 1786
output:
816089046494
result:
ok 1 number(s): "816089046494"
Test #33:
score: 15
Accepted
time: 2640ms
memory: 3568kb
input:
7 0 1685 1331 2000 2000 1941 2000 1310 1757 631 21 113 0 575
output:
633230324466
result:
ok 1 number(s): "633230324466"
Test #34:
score: 15
Accepted
time: 2581ms
memory: 3860kb
input:
8 0 650 0 1350 650 2000 1350 2000 2000 1350 2000 650 1350 0 650 0
output:
900037062925
result:
ok 1 number(s): "900037062925"