This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXK=100005;
long long mini[MAXK], maxi[MAXK], minj[MAXK], maxj[MAXK];
long long NumWhite(long long, long long, long long, long long, long long cellLen);
long long FindAns(long long n, long long k, long long cellLen);
int main()
{
long long n, k;
cin>>n>>k;
for (int i=0; i<k; i++)
scanf("%lld%lld%lld%lld", mini+i, minj+i, maxi+i, maxj+i);
long long ans=n*n;
for (int cellLen=1; cellLen*cellLen<=n; cellLen++)
if (n%cellLen==0)
{
ans=min(ans, FindAns(n, k, cellLen));
ans=min(ans, FindAns(n, k, n/cellLen));
}
cout<<ans<<'\n';
char I;
cin >> I;
return 0;
}
long long FindAns(long long n, long long k, long long cellLen)
{
if (cellLen>=n) return n*n;
long long shouldWhite=0, shouldBlack=0, isBlack=0;
for (int i=0; i<k; i++)
{
shouldWhite+=NumWhite(mini[i], minj[i], maxi[i], maxj[i], cellLen);
isBlack+=(maxi[i]-mini[i]+1)*(maxj[i]-minj[i]+1);
}
shouldBlack=(n*n-NumWhite(1, 1, n, n, cellLen))-(isBlack-shouldWhite);
return min(shouldWhite+shouldBlack, n*n-(shouldWhite+shouldBlack));
}
using ll = long long;
ll prefans(ll x, ll y, ll k, ll f)
{
ll ans;
ll ab = y / k, a;
a = (ab + 1) / 2;
a *= k;
if (ab % 2 == 0)
a += y % k;
if (x <= k)
ans = x * a;
else
{
ll aa = y - a;
ll q = x / k;
ans = aa * (q / 2) * k;
ans += a * ((q + 1) / 2) * k;
if (q % 2)
ans += aa * (x % k);
else ans += a * (x % k);
}
if (f == 1)
return ans;
else return x * y - ans;
}
long long NumWhite(long long mini, long long minj, long long maxi, long long maxj, long long len)
{
if (!mini) mini=1;
if (!minj) minj=1;
if (mini==1 && minj==1)
{
return prefans(maxi, maxj, len, 1);
long long ans=0, i=maxi, j=maxj;
ans+=len*len*(((i/len)*(j/len)+1)/2);
if ((i/len)%2)
ans+=(i%len)*((j/len)/2)*len;
else
ans+=(i%len)*((j/len+1)/2)*len;
if ((j/len)%2)
ans+=(j%len)*((i/len)/2)*len;
else
ans+=(j%len)*((i/len+1)/2)*len;
if ((i/len)%2==(j/len)%2)
ans+=(i%len)*(j%len);
return ans;
}
return NumWhite(1, 1, maxi, maxj, len)-
NumWhite(1, 1, maxi, minj-1, len)-NumWhite(1, 1, mini-1, maxj, len)+
NumWhite(1, 1, mini-1, minj-1, len);
}
Compilation message (stderr)
chessboard.cpp: In function 'int main()':
chessboard.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
15 | scanf("%lld%lld%lld%lld", mini+i, minj+i, maxi+i, maxj+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |