#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 callLen);
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\n";
}
*/
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, cellLen));
}
ans=min(ans, n*n-ans);
cout<<ans<<'\n';
char I;
cin >> I;
return 0;
}
int FindAns(int n, int k, int cellLen)
{
int shouldWhite=0, shouldBlack=0, isBlack=0, isWhite=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);
}
isWhite=n*n-isBlack;
shouldBlack=(n*n-NumWhite(1, 1, n, n, cellLen))-(isBlack-shouldWhite);
return 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
chessboard.cpp: In function 'int FindAns(int, int, int)':
chessboard.cpp:44:47: warning: variable 'isWhite' set but not used [-Wunused-but-set-variable]
int shouldWhite=0, shouldBlack=0, isBlack=0, isWhite=0;
^~~~~~~
chessboard.cpp: In function 'int main()':
chessboard.cpp:26: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 |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |