#include <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define fr first
#define sc second
#define ad push_back
using namespace std;
using ll = long long;
mt19937 rnd(348502);
const int N = 100005;
pair<ll, ll> ans[N];
void solve()
{
ll n, i, j, x, y, x1, y1, k;
cin >> n >> k;
vector<ll> baj;
baj.push_back(1);
for (i = 2; i * i < n; i++)
{
if (n % i == 0)
{
baj.push_back(i);
baj.push_back(n / i);
}
}
if (i * i == n)
baj.push_back(i);
for (i = 0; i < baj.size(); i++)
{
if ((n / baj[i]) % 2 == 1)
{
ans[i] = { ((n / baj[i]) * (n / baj[i]) / 2 + 1) * baj[i] * baj[i], (n / baj[i]) * (n / baj[i]) / 2 * baj[i] * baj[i] };
}
else
{
ans[i] = { n * n / 2, n * n / 2 };
}
}
for (i = 0; i < k; i++)
{
cin >> x >> y >> x1 >> y1;
for (j = 0; j < baj.size(); j++)
{
ll sev, sptiak;
sev = min(x / baj[j] * baj[j] + baj[j] + 1, x1 + 1) - x;
sev += max((x1 / baj[j] * baj[j]) - (x / baj[j] * baj[j] + baj[j]), 0LL) / baj[j] / 2 * baj[j];
if ((max((x1 / baj[j] * baj[j]) - (x / baj[j] * baj[j] + baj[j]), 0LL) / baj[j]) % 2 == 1)
{
sev += x1 - x1 / baj[j] * baj[j];
}
sptiak = x1 - x + 1 - sev;
ll sevy, sptiaky;
sevy = min(y / baj[j] * baj[j] + baj[j] + 1, y1 + 1) - y;
sevy += max((y1 / baj[j] * baj[j]) - (y / baj[j] * baj[j] + baj[j]), 0LL) / baj[j] / 2 * baj[j];
if ((max((y1 / baj[j] * baj[j]) - (y / baj[j] * baj[j] + baj[j]), 0LL) / baj[j]) % 2 == 1)
{
sevy += y1 - y1 / baj[j] * baj[j];
}
sptiaky = y1 - y + 1 - sevy;
ll obshisev = sev * sevy + sptiaky * sptiak;
ll obshisptiak = sevy * sptiak + sptiaky * sev;
if (((x - 1) / baj[j] + (y - 1) / baj[j]) % 2 == 1)
{
ans[j].first -= obshisev - obshisptiak;
ans[j].second -= obshisptiak - obshisev;
}
else
{
ans[j].first -= obshisptiak - obshisev;
ans[j].second -= obshisev - obshisptiak;
}
}
}
ll anss = 1e18;
for (i = 0; i < baj.size(); i++)
{
anss = min(anss, min(ans[i].first, ans[i].second));
}
cout << anss;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
return 0;
}
Compilation message
chessboard.cpp: In function 'void solve()':
chessboard.cpp:48:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (i = 0; i < baj.size(); i++)
| ~~^~~~~~~~~~~~
chessboard.cpp:62:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (j = 0; j < baj.size(); j++)
| ~~^~~~~~~~~~~~
chessboard.cpp:95:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (i = 0; i < baj.size(); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
17 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |