Submission #679361

#TimeUsernameProblemLanguageResultExecution timeMemory
679361Chal1shkanChessboard (IZhO18_chessboard)C++14
47 / 100
41 ms12748 KiB
#include <bits/stdc++.h>

# define pb push_back
# define ff first
# define ss second
# define nl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll maxn = 1e5 + 25;
const ll maxl = 22 + 0;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};

using namespace std;

ll n, k;

struct rect
{
    ll x1, y1, x2, y2;
} q[maxn];

ll pref[1007][1007];

ll get (ll x1, ll y1, ll x2, ll y2)
{
    return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
}

bool prime (ll x)
{
    for (ll i = 2; i * i <= x; ++i)
    {
        if (x % i == 0) return 0;
        if (x % (x / i) == 0) return 0; 
    }
    return 1;
}

ll solve1 (ll len)
{
    ll res1 = 0, res2 = 0;;
    ll cnt = 0;
    for (ll i = 1; i <= n; i += len)
    {
        if (cnt % 2 == 0)
        {
            bool boo = 1;
            for (ll j = 1; j <= n; j += len)
            {
                if (boo)
                {
                    res1 += (len * len) - get(i, j, i + len - 1, j + len - 1);
                    res2 += get(i, j, i + len - 1, j + len - 1);
                }
                else
                {
                    res1 +=  get(i, j, i + len - 1, j + len - 1);
                    res2 += (len * len) - get(i, j, i + len - 1, j + len - 1);
                }
                boo ^= 1;
            }
        }
        else
        {
            bool boo = 1;
            for (ll j = 1; j <= n; j += len)
            {
                if (boo)
                {
                    res1 += get(i, j, i + len - 1, j + len - 1);
                    res2 += (len * len) - get(i, j, i + len - 1, j + len - 1);
                }
                else
                {    
                    res1 += (len * len) - get(i, j, i + len - 1, j + len - 1);
                    res2 +=  get(i, j, i + len - 1, j + len - 1);
                }
                boo ^= 1;
            }
        }
        cnt++;
    }
    return min(res1, res2);
}

void ma1n ()
{
    cin >> n >> k;
    for (ll i = 1; i <= k; ++i)
    {
        cin >> q[i].x1 >> q[i].y1 >> q[i].x2 >> q[i].y2;
    }
    if (prime(n))
    {
        ll cnt0 = 0, cnt1 = 0;
        for (ll i = 1; i <= k; ++i)
        {
            if ((q[i].x1 + q[i].y1) & 1) cnt1++;
            else cnt0++;
        }
        ll ans1 = ((((n + 1) / 2) * ((n + 1) / 2)) + ((n / 2) * (n / 2))) - cnt0 + cnt1;
        ll ans2 = ((((n + 1) / 2) * (n / 2)) + (((n + 1) / 2) * (n / 2))) - cnt1 + cnt0;
        cout << min(ans1, ans2) << nl;
    }
    else if (n <= 1000)
    {
        for (ll i = 1; i <= k; ++i)
        {
            pref[q[i].x1][q[i].y1]++;
        }
        for (ll i = 1; i <= n; ++i)
        {
            for (ll j = 1; j <= n; ++j)
            {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1]; 
            }
        }
        ll ans = n * n;
        for (ll len = 1; len < n; ++len)
        {
            if (n % len == 0)
            {
                ans = min(ans, solve1(len));
            }
        }
        cout << ans << nl;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // freopen("mooyomooyo.in", "r", stdin);
    // freopen("mooyomooyo.out", "w", stdout);
    int ttt = 1;
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << nl;
        ma1n();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...