#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
#define MP make_pair
typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 222;
#define rect pair<pair<ll,ll>,pair<ll,ll>>
#define x1 first.first
#define y1 first.second
#define x2 second.first
#define y2 second.second
void solve(int TC) {
int n, k;
cin >> n >> k;
vector<rect> rectangles(k);
for (auto &r : rectangles){
cin >> r.x1 >> r.y1 >> r.x2 >> r.y2;
}
vector<ll> divisors;
for (int d = 1; d < n; d++){
if (n % d == 0) divisors.push_back(d);
}
ll ans = 1ll * n * n;
for (ll d : divisors){ /// (1, 1) = black
ll cost = ((n / d) * (n / d) + 1) / 2 * d * d;
for (auto r : rectangles){
ll i1 = (r.x1) / d + 1;
ll j1 = (r.y1) / d + 1;
ll i2 = r.x2 / d;
ll j2 = r.y2 / d;
ll id = i2 - i1 + 1, jd = j2 - j1 + 1;
debug(i1, j1, i2, j2, id, jd);
ll white = 0;
white += (id * jd / 2) * d * d;
if ((id * jd) % 2 == 1){
if ((i1 + j1) % 2 == 1) white += d * d;
}
ll h = (i1 - 1) * d - r.x1 + 1;
h = max(h, 0ll);
white += jd / 2 * h * d;
if (jd % 2 == 1){
if ((i1 + j1) % 2 == 0) white += h * d;
}
h = r.x2 - i2 * d;
h = max(h, 0ll);
white += jd / 2 * h * d;
if (jd % 2 == 1){
if ((i2 + j1) % 2 == 0) white += h * d;
}
ll w = (j1 - 1) * d - r.y1 + 1;
w = max(w, 0ll);
white += id / 2 * w * d;
if (id % 2 == 1){
if ((i1 + j1) % 2 == 0) white += w * d;
}
w = r.y2 - j2 * d;
w = max(w, 0ll);
white += id / 2 * w * d;
if (id % 2 == 1){
if ((i1 + j2) % 2 == 0) white += w * d;
}
h = (i1 - 1) * d - r.x1 + 1, w = (j1 - 1) * d - r.y1 + 1;
h = max(h, 0ll);
w = max(w, 0ll);
if ((i1 + j1) % 2 == 1) white += w * h;
w = r.y2 - j2 * d;
w = max(w, 0ll);
if ((i1 + j2) % 2 == 1) white += w * h;
h = r.x2 - i2 * d;
h = max(h, 0ll);
if ((i2 + j2) % 2 == 1) white += w * h;
h = r.x2 - i2 * d, w = (j1 - 1) * d - r.y1 + 1;
h = max(h, 0ll);
w = max(w, 0ll);
if ((i2 + j1) % 2 == 1) white += w * h;
ll black = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1) - white;
cost += white;
cost -= black;
debug(white, black);
}
ans = min(ans, cost);
debug(d, cost);
}
for (ll d : divisors){ /// (1, 1) = white
ll cost = (n / d) * (n / d) / 2 * d * d;
for (auto r : rectangles){
ll i1 = (r.x1) / d + 1;
ll j1 = (r.y1) / d + 1;
ll i2 = r.x2 / d;
ll j2 = r.y2 / d;
ll id = i2 - i1 + 1, jd = j2 - j1 + 1;
debug(i1, j1, i2, j2, id, jd);
ll white = 0;
white += (id * jd / 2) * d * d;
if ((id * jd) % 2 == 1){
if ((i1 + j1) % 2 == 0) white += d * d;
}
ll h = (i1 - 1) * d - r.x1 + 1;
h = max(h, 0ll);
white += jd / 2 * h * d;
if (jd % 2 == 1){
if ((i1 + j1) % 2 == 1) white += h * d;
}
h = r.x2 - i2 * d;
h = max(h, 0ll);
white += jd / 2 * h * d;
if (jd % 2 == 1){
if ((i2 + j1) % 2 == 1) white += h * d;
}
ll w = (j1 - 1) * d - r.y1 + 1;
w = max(w, 0ll);
white += id / 2 * w * d;
if (id % 2 == 1){
if ((i1 + j1) % 2 == 1) white += w * d;
}
w = r.y2 - j2 * d;
w = max(w, 0ll);
white += id / 2 * w * d;
if (id % 2 == 1){
if ((i1 + j2) % 2 == 1) white += w * d;
}
h = (i1 - 1) * d - r.x1 + 1, w = (j1 - 1) * d - r.y1 + 1;
h = max(h, 0ll);
w = max(w, 0ll);
if ((i1 + j1) % 2 == 0) white += w * h;
w = r.y2 - j2 * d;
w = max(w, 0ll);
if ((i1 + j2) % 2 == 0) white += w * h;
h = r.x2 - i2 * d;
h = max(h, 0ll);
if ((i2 + j2) % 2 == 0) white += w * h;
h = r.x2 - i2 * d, w = (j1 - 1) * d - r.y1 + 1;
h = max(h, 0ll);
w = max(w, 0ll);
if ((i2 + j1) % 2 == 0) white += w * h;
ll black = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1) - white;
cost += white;
cost -= black;
debug(white, black);
}
ans = min(ans, cost);
debug(d, cost);
}
cout << ans;
}
int main() {
startTime = clock();
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int TC = 1;
// cin >> TC;
for (int test = 1; test <= TC; test++) {
//debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
return 0;
}
/*
6 8
1 2 1 2
2 1 2 1
3 3 3 3
3 4 3 4
3 6 3 6
4 3 4 3
4 4 4 4
5 5 5 5
4 1
4 1 4 4
*/
Compilation message
chessboard.cpp: In function 'void solve(int)':
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:73:13: note: in expansion of macro 'debug'
73 | debug(i1, j1, i2, j2, id, jd);
| ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:131:13: note: in expansion of macro 'debug'
131 | debug(white, black);
| ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:135:9: note: in expansion of macro 'debug'
135 | debug(d, cost);
| ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:146:13: note: in expansion of macro 'debug'
146 | debug(i1, j1, i2, j2, id, jd);
| ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:204:13: note: in expansion of macro 'debug'
204 | debug(white, black);
| ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
chessboard.cpp:208:9: note: in expansion of macro 'debug'
208 | debug(d, cost);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
2252 KB |
Output is correct |
2 |
Correct |
6 ms |
716 KB |
Output is correct |
3 |
Correct |
15 ms |
2440 KB |
Output is correct |
4 |
Correct |
15 ms |
2496 KB |
Output is correct |
5 |
Correct |
21 ms |
3264 KB |
Output is correct |
6 |
Correct |
14 ms |
2104 KB |
Output is correct |
7 |
Correct |
3 ms |
712 KB |
Output is correct |
8 |
Correct |
14 ms |
2156 KB |
Output is correct |
9 |
Correct |
33 ms |
5164 KB |
Output is correct |
10 |
Correct |
19 ms |
3036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
2252 KB |
Output is correct |
2 |
Correct |
6 ms |
716 KB |
Output is correct |
3 |
Correct |
15 ms |
2440 KB |
Output is correct |
4 |
Correct |
15 ms |
2496 KB |
Output is correct |
5 |
Correct |
21 ms |
3264 KB |
Output is correct |
6 |
Correct |
14 ms |
2104 KB |
Output is correct |
7 |
Correct |
3 ms |
712 KB |
Output is correct |
8 |
Correct |
14 ms |
2156 KB |
Output is correct |
9 |
Correct |
33 ms |
5164 KB |
Output is correct |
10 |
Correct |
19 ms |
3036 KB |
Output is correct |
11 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
23 ms |
2252 KB |
Output is correct |
10 |
Correct |
6 ms |
716 KB |
Output is correct |
11 |
Correct |
15 ms |
2440 KB |
Output is correct |
12 |
Correct |
15 ms |
2496 KB |
Output is correct |
13 |
Correct |
21 ms |
3264 KB |
Output is correct |
14 |
Correct |
14 ms |
2104 KB |
Output is correct |
15 |
Correct |
3 ms |
712 KB |
Output is correct |
16 |
Correct |
14 ms |
2156 KB |
Output is correct |
17 |
Correct |
33 ms |
5164 KB |
Output is correct |
18 |
Correct |
19 ms |
3036 KB |
Output is correct |
19 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |