#include <bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
ostream& operator<<(ostream& o, pll p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll iceil(ll a, ll b){
return (a + b - 1) / b;
}
pll calc(ll l, ll r, ll t){
ll even = 0, odd = 0;
if(l / t % 2 == 0) even += iceil(l, t) * t - l;
else odd += iceil(l, t) * t - l;
l = iceil(l, t) * t;
if(l > r) return mp(even, odd);
if(r / t % 2 == 0) even += r % t + 1;
else odd += r % t + 1;
r = r / t * t - 1;
if(l > r) return mp(even, odd);
int tmp = (r - l + 1) / t;
if(l / t % 2 == 0) even += iceil(tmp, 2) * t, odd += tmp / 2 * t;
else odd += iceil(tmp, 2) * t, even += tmp / 2 * t;
return mp(even, odd);
}
vector<ll> xl, xr, yl, yr;
int n, k;
ll check(ll t){
//cerr << "check " << t << "\n";
ll b = 0, w = 0;
for(int i = 0; i < k; i++){
pll px = calc(xl[i], xr[i], t), py = calc(yl[i], yr[i], t);
//cerr << i << " " << px << " " << py << " " << xl[i] << " " << xr[i] << " " << yl[i] << " " << yr[i] << "\n";
b += px.F * py.F + px.S * py.S;
w += px.S * py.F + px.F * py.S;
}
pll tp = calc(0, n - 1, t);
ll tb = tp.F * tp.F + tp.S * tp.S;
ll tw = tp.S * tp.F + tp.F * tp.S;
//cerr << "check " << t << " " << b << " " << w << " " << tp << " " << tb << " " << tw << "\n";
return min(tb - b + w, tw - w + b);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
xl.resize(k);
xr.resize(k);
yl.resize(k);
yr.resize(k);
for(int i = 0; i < k; i++){
cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
xl[i]--;
yl[i]--;
xr[i]--;
yr[i]--;
}
ll ans = (ll) n * n;
for(int t = 1; t * t <= n; t++){
if(n % t) continue;
if(t < n) ans = min(ans, check(t));
if(n / t < n) ans = min(ans, check(t));
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2412 KB |
Output is correct |
2 |
Correct |
8 ms |
1260 KB |
Output is correct |
3 |
Correct |
18 ms |
2392 KB |
Output is correct |
4 |
Correct |
21 ms |
2604 KB |
Output is correct |
5 |
Correct |
24 ms |
2924 KB |
Output is correct |
6 |
Correct |
16 ms |
2156 KB |
Output is correct |
7 |
Correct |
5 ms |
748 KB |
Output is correct |
8 |
Correct |
16 ms |
2156 KB |
Output is correct |
9 |
Correct |
39 ms |
4332 KB |
Output is correct |
10 |
Correct |
26 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2412 KB |
Output is correct |
2 |
Correct |
8 ms |
1260 KB |
Output is correct |
3 |
Correct |
18 ms |
2392 KB |
Output is correct |
4 |
Correct |
21 ms |
2604 KB |
Output is correct |
5 |
Correct |
24 ms |
2924 KB |
Output is correct |
6 |
Correct |
16 ms |
2156 KB |
Output is correct |
7 |
Correct |
5 ms |
748 KB |
Output is correct |
8 |
Correct |
16 ms |
2156 KB |
Output is correct |
9 |
Correct |
39 ms |
4332 KB |
Output is correct |
10 |
Correct |
26 ms |
2668 KB |
Output is correct |
11 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |