#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,q;cin>>n>>q;
vector<int> v;
vector<int> a[2];
for(int i=1;i<n;i++){
if(n%i==0){
v.push_back(i);
int p=n/i;
if(p&1){
a[0].push_back((p*p-1)*i*i/2);
a[1].push_back((p*p+1)*i*i/2);
}
else{
a[0].push_back(n*n/2);
a[1].push_back(n*n/2);
}
}
}
auto solve=[&](int a,int b,int c,int d,int k)->long long {
if(c<a||d<b){
return 0;
}
int a1=a/k;
int b1=b/k;
int c1=c/k;
int d1=d/k;
int ans=0;
if((d1-b1)*(c1-a1)%2==1){
if((a1^b1)&1){
ans+=k*k;
}
else{
ans-=k*k;
}
}
int up=c-c1*k+1;
int right=d-d1*k+1;
if((c1-a1)%2==1){
if((a1^d1)&1){
ans+=k*right;
}
else{
ans-=k*right;
}
}
if((d1-b1)%2==1){
if((b1^c1)&1){
ans+=k*up;
}
else{
ans-=k*up;
}
}
if((c1^d1)&1){
ans+=up*right;
}
else{
ans-=up*right;
}
//cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<ans<<'\n';
return ans;
};
int sz=v.size();
for(int i=0;i<q;i++){
int c,d,e,f;
cin>>c>>d>>e>>f;
c--;d--;e--;f--;
for(int j=0;j<sz;j++){
int k=v[j];
int c1=c/k;
int d1=d/k;
int e1=e/k;
int f1=f/k;
int t=solve(c1*k,d1*k,e,f,k)-solve(c1*k,d1*k,c-1,f,k)-solve(c1*k,d1*k,e,d-1,k)+solve(c1*k,d1*k,c-1,d-1,k);
//cout<<t<<'\n';
a[0][j]+=t;
a[1][j]-=t;
}
}
int ans=n*n;
for(int j=0;j<sz;j++){
ans=min(ans,a[0][j]);
ans=min(ans,a[1][j]);
//cout<<v[j]<<' '<<a[0][j]<<' '<<a[1][j]<<endl;
}
cout<<ans<<endl;
return 0;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
*/
Compilation message
chessboard.cpp: In function 'int32_t main()':
chessboard.cpp:78:17: warning: unused variable 'e1' [-Wunused-variable]
78 | int e1=e/k;
| ^~
chessboard.cpp:79:17: warning: unused variable 'f1' [-Wunused-variable]
79 | int f1=f/k;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
16 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |