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;
const LL MAXN=100005;
LL n,k,ans,xl[MAXN],xr[MAXN],yl[MAXN],yr[MAXN];
LL prefix(LL x,LL d){
//consider ujung kiri atas hitam
//di chessboard yang ukuran perwarnanya d*d
//count hitam di prefix dari x
return (x/(2*d))*d+min(x%(2*d),d);
}
void calc(LL d){
if(d==n)return;
LL hitam=0,putih=0,genap=((n/d)*(n/d)+1)/2,ganjil=((n/d)*(n/d))/2;
putih=ganjil*d*d; //consider ujung kiri atas putih
hitam=genap*d*d; //consider ujung kiri atas hitam
for(LL i=1;i<=k;i++){
LL blackrow=prefix(xr[i],d)-prefix(xl[i]-1,d),whiterow=xr[i]-xl[i]+1-blackrow;
LL blackcolumn=prefix(yr[i],d)-prefix(yl[i]-1,d),whitecolumn=yr[i]-yl[i]+1-blackcolumn;
//anggep consider ujung kiri atas hitam kaya di gambar,count black in this subrectangle
LL black=blackrow*blackcolumn+whiterow*whitecolumn;
LL white=(xr[i]-xl[i]+1)*(yr[i]-yl[i]+1)-black;
hitam-=black;
putih+=black;
hitam+=white;
putih-=white;
}
ans=min(ans,min(hitam,putih));
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin >> n >> k;
for(LL i=1;i<=k;i++){
cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
}
ans=n*n;
for(LL i=1;i*i<=n;i++){
if(n%i!=0)continue;
calc(i);calc(n/i);
}
cout << ans << '\n';
return 0;
}
# | 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... |