#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define int long long
const int maxn=1e5+9;
struct rect{
int x1,y1,x2,y2;
}a[maxn];
long long cal(int len,bool odd){
if (!odd){
if (len%2==1)len--;
return (len-2)/2+1;
}
else {
if (len%2==0)len--;
return (len-1)/2+1;
}
}
long long x[2];
long long cal(int l,int r,bool odd){
if (odd){
if (r%2==0)r--;
if (l%2==0)l++;
if (l>r)return 0;
return (r-l)/2+1;
}
else {
if (r%2==1)r--;
if (l%2==1)l++;
if (l>r)return 0;
return (r-l)/2+1;
}
}
pair<long long,long long> solve(int x1, int y1,int i){
memset(x,0,sizeof(x));
if ((x1+i-1)/i==(y1+i-1/i)/i){
x[((x1+i-1)/i)%2]+=y1-x1+1;
//cout<<"yay";
}
else {
int len=(x1+i-1)/i;
//cerr<<len<<'\n';
x[len%2]+=(len)*i-x1+1;
x1=(len)*i+1;
x[((y1+i-1)/i)%2]+=(y1)%i;
y1-=(y1%i);
//cout<<x[0]<<" "<<y1<<'\n';
if (x1>y1)return {x[0],x[1]};
int l1=((x1+i-1)/i),l2=y1/i;
x[0]+=1ll*cal(l1,l2,0)*i;
x[1]+=1ll*cal(l1,l2,1)*i;
}
return {x[0],x[1]};
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
long long n;
int k;
cin>>n>>k;
for1(i,1,k)cin>>a[i].x1>>a[i].y1>>a[i].x2>>a[i].y2;
long long ans=n*n;
for1(i,1,n-1){
if (n%i!=0)continue;
long long len=n/i;
long long sum=cal(len,1)*cal(len,1)*i*i+cal(len,0)*cal(len,0)*i*i;
long long sum2=cal(len,1)*cal(len,0)*i*i+cal(len,0)*cal(len,1)*i*i;
for1(j,1,k){
int x1=a[j].x1,y1=a[j].y1,x2=a[j].x2,y2=a[j].y2;
pair<long long,long long>cordx=solve(x1,x2,i),cordy=solve(y1,y2,i);
//cout<<cordx.fi<<" "<<cordx.se<<" "<<cordy.fi<<" "<<cordy.se<<'\n';
sum-=cordx.fi*cordy.fi;
sum-=cordx.se*cordy.se;
sum+=cordx.fi*cordy.se;
sum+=cordx.se*cordy.fi;
sum2+=cordx.fi*cordy.fi;
sum2+=cordx.se*cordy.se;
sum2-=cordx.fi*cordy.se;
sum2-=cordx.se*cordy.fi;
}
ans=min(ans,sum);
ans=min(ans,sum2);
//cout<<i<<" "<<sum<<" "<<sum2<<'\n';
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3696 KB |
Output is correct |
2 |
Correct |
6 ms |
2828 KB |
Output is correct |
3 |
Correct |
11 ms |
3420 KB |
Output is correct |
4 |
Correct |
12 ms |
3416 KB |
Output is correct |
5 |
Correct |
15 ms |
3676 KB |
Output is correct |
6 |
Correct |
10 ms |
3420 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
10 ms |
3412 KB |
Output is correct |
9 |
Correct |
25 ms |
4440 KB |
Output is correct |
10 |
Correct |
14 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
500 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
488 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
500 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
488 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
12 ms |
2908 KB |
Output is correct |
17 |
Correct |
30 ms |
4020 KB |
Output is correct |
18 |
Correct |
29 ms |
4444 KB |
Output is correct |
19 |
Correct |
96 ms |
4216 KB |
Output is correct |
20 |
Correct |
101 ms |
4556 KB |
Output is correct |
21 |
Correct |
19 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
18 ms |
3164 KB |
Output is correct |
24 |
Correct |
26 ms |
4188 KB |
Output is correct |
25 |
Correct |
5 ms |
2904 KB |
Output is correct |
26 |
Correct |
17 ms |
3324 KB |
Output is correct |
27 |
Correct |
25 ms |
3440 KB |
Output is correct |
28 |
Correct |
29 ms |
4344 KB |
Output is correct |
29 |
Correct |
8 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3696 KB |
Output is correct |
2 |
Correct |
6 ms |
2828 KB |
Output is correct |
3 |
Correct |
11 ms |
3420 KB |
Output is correct |
4 |
Correct |
12 ms |
3416 KB |
Output is correct |
5 |
Correct |
15 ms |
3676 KB |
Output is correct |
6 |
Correct |
10 ms |
3420 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
10 ms |
3412 KB |
Output is correct |
9 |
Correct |
25 ms |
4440 KB |
Output is correct |
10 |
Correct |
14 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
500 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
488 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
12 ms |
2908 KB |
Output is correct |
27 |
Correct |
30 ms |
4020 KB |
Output is correct |
28 |
Correct |
29 ms |
4444 KB |
Output is correct |
29 |
Correct |
96 ms |
4216 KB |
Output is correct |
30 |
Correct |
101 ms |
4556 KB |
Output is correct |
31 |
Correct |
19 ms |
3932 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
18 ms |
3164 KB |
Output is correct |
34 |
Correct |
26 ms |
4188 KB |
Output is correct |
35 |
Correct |
5 ms |
2904 KB |
Output is correct |
36 |
Correct |
17 ms |
3324 KB |
Output is correct |
37 |
Correct |
25 ms |
3440 KB |
Output is correct |
38 |
Correct |
29 ms |
4344 KB |
Output is correct |
39 |
Correct |
8 ms |
3164 KB |
Output is correct |
40 |
Correct |
2 ms |
348 KB |
Output is correct |
41 |
Correct |
80 ms |
4180 KB |
Output is correct |
42 |
Correct |
31 ms |
4688 KB |
Output is correct |
43 |
Correct |
49 ms |
4328 KB |
Output is correct |
44 |
Correct |
31 ms |
4340 KB |
Output is correct |
45 |
Correct |
27 ms |
4944 KB |
Output is correct |
46 |
Correct |
89 ms |
4700 KB |
Output is correct |
47 |
Correct |
23 ms |
4188 KB |
Output is correct |
48 |
Correct |
39 ms |
4320 KB |
Output is correct |
49 |
Correct |
28 ms |
4200 KB |
Output is correct |
50 |
Correct |
380 ms |
4692 KB |
Output is correct |
51 |
Correct |
413 ms |
5204 KB |
Output is correct |
52 |
Correct |
373 ms |
4448 KB |
Output is correct |
53 |
Correct |
398 ms |
4724 KB |
Output is correct |
54 |
Correct |
368 ms |
4616 KB |
Output is correct |
55 |
Correct |
414 ms |
4956 KB |
Output is correct |
56 |
Correct |
361 ms |
4672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
19 ms |
3696 KB |
Output is correct |
10 |
Correct |
6 ms |
2828 KB |
Output is correct |
11 |
Correct |
11 ms |
3420 KB |
Output is correct |
12 |
Correct |
12 ms |
3416 KB |
Output is correct |
13 |
Correct |
15 ms |
3676 KB |
Output is correct |
14 |
Correct |
10 ms |
3420 KB |
Output is correct |
15 |
Correct |
4 ms |
2652 KB |
Output is correct |
16 |
Correct |
10 ms |
3412 KB |
Output is correct |
17 |
Correct |
25 ms |
4440 KB |
Output is correct |
18 |
Correct |
14 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
488 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
12 ms |
2908 KB |
Output is correct |
35 |
Correct |
30 ms |
4020 KB |
Output is correct |
36 |
Correct |
29 ms |
4444 KB |
Output is correct |
37 |
Correct |
96 ms |
4216 KB |
Output is correct |
38 |
Correct |
101 ms |
4556 KB |
Output is correct |
39 |
Correct |
19 ms |
3932 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
18 ms |
3164 KB |
Output is correct |
42 |
Correct |
26 ms |
4188 KB |
Output is correct |
43 |
Correct |
5 ms |
2904 KB |
Output is correct |
44 |
Correct |
17 ms |
3324 KB |
Output is correct |
45 |
Correct |
25 ms |
3440 KB |
Output is correct |
46 |
Correct |
29 ms |
4344 KB |
Output is correct |
47 |
Correct |
8 ms |
3164 KB |
Output is correct |
48 |
Correct |
2 ms |
348 KB |
Output is correct |
49 |
Correct |
80 ms |
4180 KB |
Output is correct |
50 |
Correct |
31 ms |
4688 KB |
Output is correct |
51 |
Correct |
49 ms |
4328 KB |
Output is correct |
52 |
Correct |
31 ms |
4340 KB |
Output is correct |
53 |
Correct |
27 ms |
4944 KB |
Output is correct |
54 |
Correct |
89 ms |
4700 KB |
Output is correct |
55 |
Correct |
23 ms |
4188 KB |
Output is correct |
56 |
Correct |
39 ms |
4320 KB |
Output is correct |
57 |
Correct |
28 ms |
4200 KB |
Output is correct |
58 |
Correct |
380 ms |
4692 KB |
Output is correct |
59 |
Correct |
413 ms |
5204 KB |
Output is correct |
60 |
Correct |
373 ms |
4448 KB |
Output is correct |
61 |
Correct |
398 ms |
4724 KB |
Output is correct |
62 |
Correct |
368 ms |
4616 KB |
Output is correct |
63 |
Correct |
414 ms |
4956 KB |
Output is correct |
64 |
Correct |
361 ms |
4672 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
462 ms |
4644 KB |
Output is correct |
68 |
Correct |
458 ms |
5580 KB |
Output is correct |
69 |
Correct |
397 ms |
4692 KB |
Output is correct |
70 |
Correct |
432 ms |
5416 KB |
Output is correct |
71 |
Correct |
434 ms |
5344 KB |
Output is correct |
72 |
Correct |
431 ms |
5256 KB |
Output is correct |
73 |
Correct |
424 ms |
5136 KB |
Output is correct |
74 |
Correct |
471 ms |
5528 KB |
Output is correct |
75 |
Correct |
436 ms |
5324 KB |
Output is correct |
76 |
Correct |
465 ms |
5636 KB |
Output is correct |
77 |
Correct |
87 ms |
5892 KB |
Output is correct |
78 |
Correct |
35 ms |
5356 KB |
Output is correct |
79 |
Correct |
61 ms |
5104 KB |
Output is correct |
80 |
Correct |
65 ms |
5376 KB |
Output is correct |
81 |
Correct |
60 ms |
4864 KB |
Output is correct |
82 |
Correct |
52 ms |
5608 KB |
Output is correct |
83 |
Correct |
44 ms |
5212 KB |
Output is correct |
84 |
Correct |
284 ms |
5764 KB |
Output is correct |
85 |
Correct |
482 ms |
5856 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
468 KB |
Output is correct |
88 |
Correct |
490 ms |
5940 KB |
Output is correct |
89 |
Correct |
98 ms |
2904 KB |
Output is correct |
90 |
Correct |
1 ms |
600 KB |
Output is correct |