#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
const int N = 2e6 + 10;
const ll M = 1e8;
const ll inf = 1e18;
const ll mod = 1e9;
const double Pi = acos(-1);
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
struct coor {
ll x1, y1, x2, y2;
} a[N];
ll n, m, cnt[N], dp[N];
const void solve(/*Armashka*/) {
cin >> n >> m;
for (int i = 1; i <= m; ++ i) cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2;
vector <ll> v;
v.pb(1);
for (int i = 2; i * i <= n; ++ i) {
if (n % i == 0) {
v.pb(i);
if (i * i != n) v.pb(i * i);
}
}
for (int i = 0; i < v.sz; ++ i) dp[i] = n * n - n * n / (v[i] * v[i]) / 2 * (v[i] * v[i]);
for (int i = 1; i <= m; ++ i) {
ll x1 = a[i].x1, x2 = a[i].x2, y1 = a[i].y1, y2 = a[i].y2;
for (int j = 0; j < v.sz; ++ j) {
ll cur = v[j];
ll X = x2 / (cur * 2) * cur;
X += min(cur, x2 % (cur * 2));
ll XX = (x1 - 1) / (cur * 2) * cur;
XX += min(cur, (x1 - 1) % (cur * 2));
X -= XX;
ll Y = y2 / (cur * 2) * cur;
Y += min(cur, y2 % (cur * 2));
ll YY = (y1 - 1) / (cur * 2) * cur;
YY += min(cur, (y1 - 1) % (cur * 2));
Y -= YY;
ll X1 = (x2 - x1 + 1) - X;
ll Y1 = (y2 - y1 + 1) - Y;
dp[j] -= X * Y + X1 * Y1;
dp[j] += X * Y1 + X1 * Y;
}
}
ll ans = inf;
for (int i = 0; i < v.sz; ++ i) {
ans = min({ans, dp[i], n * n - dp[i]});
}
cout << ans << "\n";
}
signed main() {
fast;
//freopen("rmq.in", "r", stdin);
//freopen("rmq.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt --) {
solve();
}
}
// Uaqyt, otedi, ketedi, mulde bilinbei ulger
// Kunder seni kutpeidi, susyz ospeidi gulder
Compilation message
chessboard.cpp:3: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
3 | #pragma comment(linker, "/stack:200000000")
|
chessboard.cpp: In function 'const void solve()':
chessboard.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < v.sz; ++ i) dp[i] = n * n - n * n / (v[i] * v[i]) / 2 * (v[i] * v[i]);
| ^
chessboard.cpp:51:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int j = 0; j < v.sz; ++ j) {
| ^
chessboard.cpp:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < v.sz; ++ i) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
324 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
3784 KB |
Output is correct |
2 |
Correct |
6 ms |
1136 KB |
Output is correct |
3 |
Correct |
13 ms |
2404 KB |
Output is correct |
4 |
Correct |
14 ms |
2508 KB |
Output is correct |
5 |
Correct |
19 ms |
3312 KB |
Output is correct |
6 |
Correct |
12 ms |
2224 KB |
Output is correct |
7 |
Correct |
3 ms |
696 KB |
Output is correct |
8 |
Correct |
12 ms |
2112 KB |
Output is correct |
9 |
Correct |
29 ms |
5156 KB |
Output is correct |
10 |
Correct |
17 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
3784 KB |
Output is correct |
2 |
Correct |
6 ms |
1136 KB |
Output is correct |
3 |
Correct |
13 ms |
2404 KB |
Output is correct |
4 |
Correct |
14 ms |
2508 KB |
Output is correct |
5 |
Correct |
19 ms |
3312 KB |
Output is correct |
6 |
Correct |
12 ms |
2224 KB |
Output is correct |
7 |
Correct |
3 ms |
696 KB |
Output is correct |
8 |
Correct |
12 ms |
2112 KB |
Output is correct |
9 |
Correct |
29 ms |
5156 KB |
Output is correct |
10 |
Correct |
17 ms |
3064 KB |
Output is correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
324 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |