This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Arayi
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 0, -1, 0, 1, -1, -1, 1, 1, 0 };
int dy[] = { -1, 0, 1, 0, -1, 1, -1, 1, 0 };
const int N = 5e6 + 30;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = log2(3);
const int T = 1000;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template<class T>
void maxi(T& a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T& a, T b)
{
a = min(a, b);
}
lli n, k;
lli x1[N], x2[N], y1[N], y2[N];
lli sum, pat = mod * mod;
bool sp(lli i, lli j, lli x)
{
if (((i / x) + (j / x)) % 2 == 0) return 1;
return 0;
}
lli f1(lli x1, lli y1, lli x2, lli y2, lli x)
{
return (x2 - x1 + 1LL) * 1LL * (y2 - y1 + 1LL) * sp(x1, y1, x);
}
lli f2(lli x1, lli y1, lli x2, lli y2, lli x)
{
lli l = (y1 - 1 + x) / x, r = y2 / x;
l *= x, r *= x;
lli sm = f1(x1, y1, x2, l - 1, x) + f1(x1, r, x2, y2, x);
lli s = (r - l) / x; s = (s + sp(x1, l, x)) / 2;
sm += s * x * 1LL * (x2 - x1 + 1LL);
return sm;
}
int main()
{
fastio;
cin >> n >> k;
for (int i = 0; i < k; i++)
{
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
x1[i]--, x2[i]--, y1[i]--, y2[i]--;
sum += (x2[i] - x1[i] + 1) * 1LL * (y2[i] - y1[i] + 1);
}
for (lli x = 1; x < n; x++)
{
if (n % x) continue;
lli sm = 0;
lli ssp = ((n / x) * (n / x) + 1) / 2LL;
ssp *= x * x;
//cout << x << " " << ssp << endl;
for (int i = 0; i < k; i++)
{
bool bl1 = ((x1[i] / x) != (x2[i] / x));
bool bl2 = ((y1[i] / x) != (y2[i] / x));
if (!bl1 && !bl2) sm += f1(x1[i], y1[i], x2[i], y2[i], x);
else if (bl1 && !bl2) sm += f2(y1[i], x1[i], y2[i], x2[i], x);
else if (!bl1 && bl2) sm += f2(x1[i], y1[i], x2[i], y2[i], x);
else
{
lli i2 = (x2[i] / x) * x, j2 = (y2[i] / x) * x;
lli i1 = ((x1[i] - 1 + x) / x) * x, j1 = ((y1[i] - 1 + x) / x) * x;
sm += f2(y1[i], i1, j1 - 1, x2[i], x) + f2(i2, j1, x2[i], y2[i], x) + f2(j2, x1[i], y2[i], i2 - 1, x) + f2(x1[i], y1[i], i1 - 1, j2 - 1, x);
lli s = ((i2 - i1) / x) * 1LL * ((j2 - j1) / x);
sm += x * 1LL * x * ((s + sp(i1, j1, x)) / 2LL);
}
}
//cout << sm << endl;
lli s = n * n - ssp - sum + sm;
//cout << s << endl;
mini(pat, s + sm);
mini(pat, n * n - s - sm);
}
cout << pat << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |