#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<random>
#include<cmath>
#include<stack>
#include<map>
#include <iomanip>
#include <queue>
#include <set>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll mod = 1e9 + 7;
ll pv(ll a, ll b) {
if (b == 0)return 1;
ll res = (pv(a, b / 2));
if (b % 2) {
return (((res * res) % mod) * (a % mod)) % mod;
}
else {
return (res * res) % mod;
}
}
ll gcd(ll n1, ll n2)
{
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
void solve() {
int n, k1; cin >> n >> k1;
int baj = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0)baj++;
}
if (baj == 2) {
//cout << "APER" << endl;
ll sev = 0, hop = 0, sev1 = 0, hop1 = 0;
for (ll i = 0; i < k1; i++) {
ll x, y, x1, y1; cin >> x >> y >> x1 >> y1;
//st.insert(x * n + y);
if (x % 2 == 1 && y % 2 == 0) {
sev++;
}
else if (x % 2 == 0 && y % 2 == 1) {
sev++;
}
else hop++;
if (x % 2 == 1 && y % 2 == 1) {
sev1++;
}
else if (x % 2 == 0 && y % 2 == 0) {
sev1++;
}
else hop1++;
}
ll aper = (n * n),kor=k1;
ll mn = min({ (aper / 2 - sev) + hop, ((aper + 1) / 2 - sev1) + hop1,n * n - kor });
cout << mn << endl;
return;
}
// global depq
vector<vector<int>>pref(n + 1, vector<int>(n + 1));
for (int i = 0; i < k1; i++) {
int x, y, x1, y1; cin >> x >> y >> x1 >> y1;
pref[x][y]++;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
pref[i][j] += (pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1]);
}
}
int cnt = 0, mn = 1e9;
for (int k = 1; k < n; k++) {
if (n % k == 0) {
int ans1 = 0;
for (int i = k; i <= n; i += k) {
if (cnt % 2 == 0) {
int cnt1 = 0;
for (int j = k; j <= n; j += k) {
int qarakusi = 0;
if (cnt1 % 2 == 0) {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (k * k - qarakusi);
}
else {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (qarakusi);
}
cnt1++;
}
}
else {
int cnt1 = 0;
for (int j = k; j <= n; j += k) {
int qarakusi = 0;
if (cnt1 % 2 == 1) {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (k * k - qarakusi);
}
else {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (qarakusi);
}
cnt1++;
}
}
cnt++;
}
mn = min(mn, ans1);
}
}
cnt = 0;
for (int k = 1; k < n; k++) {
if (n % k == 0) {
int ans1 = 0;
for (int i = k; i <= n; i += k) {
if (cnt % 2 == 1) {
int cnt1 = 0;
for (int j = k; j <= n; j += k) {
int qarakusi = 0;
if (cnt1 % 2 == 0) {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (k * k - qarakusi);
}
else {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (qarakusi);
}
cnt1++;
}
}
else {
int cnt1 = 0;
for (int j = k; j <= n; j += k) {
int qarakusi = 0;
if (cnt1 % 2 == 1) {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (k * k - qarakusi);
}
else {
qarakusi = pref[i][j] - pref[i - k][j] - pref[i][j - k] + pref[i - k][j - k];
ans1 += (qarakusi);
}
cnt1++;
}
}
cnt++;
}
mn = min(mn, ans1);
}
}
cout << mn << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cin.tie(nullptr);
ll _ = 1;
//cin >> _;
while (_--)
{
solve();
}
}
# | 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... |