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;
int mini[MAXK], maxi[MAXK], minj[MAXK], maxj[MAXK];
int NumWhite(int, int, int, int, int cellLen);
int FindAns(int n, int k, int cellLen);
int main()
{
//int len;
//cin>>len;
//while (true)
//{
// int mini, minj, maxi, maxj;
// cin>>mini>>minj>>maxi>>maxj;
// cout<<"There are "<<NumWhite(mini, minj, maxi, maxj, len)
// <<" white cells in that subrectangle.\n";
// int realAns=0;
// for (int i=mini; i<=maxi; i++)
// for (int j=minj; j<=maxj; j++)
// if (((i+len-1)/len+(j+len-1)/len)%2==0)
// realAns++;
// if (NumWhite(mini, minj, maxi, maxj, len)==realAns)
// cout<<"OK\n";
// else
// cout<<"\aWA\n Should be "<<realAns<<".\n";
// cout<<endl;
//}
int n, k;
cin>>n>>k;
for (int i=0; i<k; i++)
scanf("%d%d%d%d", mini+i, minj+i, maxi+i, maxj+i);
int 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;
}
int FindAns(int n, int k, int cellLen)
{
if (cellLen>=n) return n*n;
int 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));
}
int NumWhite(int mini, int minj, int maxi, int maxj, int len)
{
if (!mini) mini=1;
if (!minj) minj=1;
if (mini==1 && minj==1)
{
int 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:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", 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... |