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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
struct Rect { int y1, x1, y2, x2; };
Rect A[MAXN+10];
int N, K;
ll f(int y, int x)
{
if(y<0 || x<0) return 0;
ll t=(ll)(y+1)*(x+1);
return (t+1)/2;
}
ll g(int y, int x, int it)
{
if(y<0 || x<0) return 0;
int yy=(y+1)/it-1, xx=(x+1)/it-1;
ll ret=0;
ret+=f(yy, xx)*it*it;
ret+=(ll)(x-(xx*it+it-1))*(f(yy, xx+1)-f(yy, xx))*it;
ret+=(ll)(y-(yy*it+it-1))*(f(yy+1, xx)-f(yy, xx))*it;
if((yy+xx)%2==0) ret+=(ll)(x-(xx*it+it-1))*(y-(yy*it+it-1));
//printf("%d %d %d => %d %d %lld\n", y, x, it, yy, xx, ret);
return ret;
}
int main()
{
int i, j;
scanf("%d%d", &N, &K);
for(i=1; i<=K; i++) scanf("%d%d%d%d", &A[i].y1, &A[i].x1, &A[i].y2, &A[i].x2), A[i].y1--, A[i].x1--, A[i].y2--, A[i].x2--;
vector<int> V;
for(i=1; i<N; i++) if(N%i==0) V.push_back(i);
ll ans=1e18;
for(auto it : V)
{
ll a=0, b=0, c=0;
for(i=1; i<=K; i++)
{
ll t=g(A[i].y2, A[i].x2, it)-g(A[i].y1-1, A[i].x2, it)-g(A[i].y2, A[i].x1-1, it)+g(A[i].y1-1, A[i].x1-1, it);
a+=t;
b+=(ll)(A[i].y2-A[i].y1+1)*(A[i].x2-A[i].x1+1);
}
c=((ll)(N/it)*(N/it)+1)/2*it*it-a;
b=N*N-b;
a+=b-c;
//printf("%lld\n", a);
ans=min(ans, min(a, N*N-a));
}
printf("%lld\n", ans);
}
Compilation message (stderr)
chessboard.cpp: In function 'int main()':
chessboard.cpp:37:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
chessboard.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
chessboard.cpp:40:112: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=K; i++) scanf("%d%d%d%d", &A[i].y1, &A[i].x1, &A[i].y2, &A[i].x2), A[i].y1--, A[i].x1--, A[i].y2--, A[i].x2--;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# | 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... |