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 long long 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 (long long i=0; i<k; i++)
scanf("%lld%lld%lld%lld", mini+i, minj+i, maxi+i, maxj+i);
long long ans=n*n;
for (long long cellLen=1LL; 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 (long long i=0; i<k; i++)
{
shouldWhite+=NumWhite(mini[i], minj[i], maxi[i], maxj[i], cellLen);
isBlack+=(maxi[i]-mini[i]+1LL)*(maxj[i]-minj[i]+1LL);
}
shouldBlack=(n*n-NumWhite(1LL, 1LL, n, n, cellLen))-(isBlack-shouldWhite);
return min(shouldWhite+shouldBlack, n*n-(shouldWhite+shouldBlack));
}
long long NumWhite(long long mini, long long minj, long long maxi, long long maxj, long long len)
{
if (!mini) mini=1LL;
if (!minj) minj=1LL;
if (mini==1LL && minj==1LL)
{
long long ans=0, i=maxi, j=maxj;
ans+=len*len*(((i/len)*(j/len)+1LL)/2LL);
if ((i/len)%2LL)
ans+=(i%len)*((j/len)/2LL)*len;
else
ans+=(i%len)*((j/len+1LL)/2LL)*len;
if ((j/len)%2LL)
ans+=(j%len)*((i/len)/2LL)*len;
else
ans+=(j%len)*((i/len+1LL)/2LL)*len;
if ((i/len)%2LL==(j/len)%2LL)
ans+=(i%len)*(j%len);
return ans;
}
return NumWhite(1LL, 1LL, maxi, maxj, len)-
NumWhite(1LL, 1LL, maxi, minj-1LL, len)-NumWhite(1LL, 1LL, mini-1LL, maxj, len)+
NumWhite(1LL, 1LL, mini-1LL, minj-1LL, len);
}
//#include <iostream>
//#include <stdio.h>
//#include <set>
//using namespace std;
//const long long MAXK=1LL00005;
//
//long long mini[MAXK], maxi[MAXK], minj[MAXK], maxj[MAXK];
//
//bool IsWhite(long long, long long);
//long long FindAns(long long n, long long k);
//int main()
//{
// long long n, k;
// cin>>n>>k;
// for (long long i=0; i<k; i++)
// scanf("%lld%lld%lld%lld", mini+i, minj+i, maxi+i, maxj+i);
// cout<<FindAns(n, k)<<'\n';
//
// char I;
// cin >> I;
// return 0;
//}
//
//long long FindAns(long long n, long long k)
//{
// long long shouldWhite=0, shouldBlack=(n*n)/2LL;
// for (long long i=0; i<k; i++)
// {
// if (IsWhite(maxi[i], maxj[i]))
// shouldWhite++;
// else
// shouldBlack--;
// }
// return min(1LLLL+shouldWhite+shouldBlack, n*n-(shouldWhite+shouldBlack));
//}
//
//bool IsWhite(long long i, long long j)
//{
// return ((i+j)%2LL==0);
//}
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]
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... |