답안 #378575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
378575 2021-03-17T01:54:05 Z wiwiho Chessboard (IZhO18_chessboard) C++14
100 / 100
516 ms 3692 KB
#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;
}

void checkcalc(ll l, ll r, ll t, pll c){
    ll even = 0, odd = 0;
    for(ll i = l; i <= r; i++){
        if(i / t % 2 == 0) even++;
        else odd++;
    }
    assert(mp(even, odd) == c);
}

pll calc(ll l, ll r, ll t){
    ll even = 0, odd = 0;

    if(l / t == r / t){
        if(l / t % 2 == 0) even = r - l + 1;
        else odd = r - l + 1;
        return mp(even, odd);
    }

    if(l / t % 2 == 0) even += iceil(l, t) * t - l;
    else odd += iceil(l, t) * t - l;
    l = iceil(l, t) * t;

    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);
        //checkcalc(xl[i], xr[i], t, px);
        //checkcalc(yl[i], yr[i], t, py);
        //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);
    //checkcalc(0, n - 1, t, tp);
    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(n / t));
    }
    cout << ans << "\n";


    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2284 KB Output is correct
2 Correct 7 ms 876 KB Output is correct
3 Correct 16 ms 1516 KB Output is correct
4 Correct 19 ms 1772 KB Output is correct
5 Correct 23 ms 2028 KB Output is correct
6 Correct 16 ms 1388 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 15 ms 1388 KB Output is correct
9 Correct 37 ms 3180 KB Output is correct
10 Correct 21 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 12 ms 1132 KB Output is correct
17 Correct 29 ms 2944 KB Output is correct
18 Correct 41 ms 3436 KB Output is correct
19 Correct 104 ms 3052 KB Output is correct
20 Correct 116 ms 3564 KB Output is correct
21 Correct 28 ms 2924 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 23 ms 1644 KB Output is correct
24 Correct 37 ms 3180 KB Output is correct
25 Correct 5 ms 620 KB Output is correct
26 Correct 23 ms 2176 KB Output is correct
27 Correct 32 ms 2412 KB Output is correct
28 Correct 39 ms 3436 KB Output is correct
29 Correct 12 ms 1388 KB Output is correct
30 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2284 KB Output is correct
2 Correct 7 ms 876 KB Output is correct
3 Correct 16 ms 1516 KB Output is correct
4 Correct 19 ms 1772 KB Output is correct
5 Correct 23 ms 2028 KB Output is correct
6 Correct 16 ms 1388 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 15 ms 1388 KB Output is correct
9 Correct 37 ms 3180 KB Output is correct
10 Correct 21 ms 1900 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 12 ms 1132 KB Output is correct
27 Correct 29 ms 2944 KB Output is correct
28 Correct 41 ms 3436 KB Output is correct
29 Correct 104 ms 3052 KB Output is correct
30 Correct 116 ms 3564 KB Output is correct
31 Correct 28 ms 2924 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 23 ms 1644 KB Output is correct
34 Correct 37 ms 3180 KB Output is correct
35 Correct 5 ms 620 KB Output is correct
36 Correct 23 ms 2176 KB Output is correct
37 Correct 32 ms 2412 KB Output is correct
38 Correct 39 ms 3436 KB Output is correct
39 Correct 12 ms 1388 KB Output is correct
40 Correct 2 ms 364 KB Output is correct
41 Correct 95 ms 3052 KB Output is correct
42 Correct 44 ms 3308 KB Output is correct
43 Correct 63 ms 2924 KB Output is correct
44 Correct 45 ms 3180 KB Output is correct
45 Correct 40 ms 3436 KB Output is correct
46 Correct 107 ms 3180 KB Output is correct
47 Correct 35 ms 3084 KB Output is correct
48 Correct 53 ms 3052 KB Output is correct
49 Correct 40 ms 2924 KB Output is correct
50 Correct 422 ms 3180 KB Output is correct
51 Correct 450 ms 3436 KB Output is correct
52 Correct 418 ms 3180 KB Output is correct
53 Correct 448 ms 3404 KB Output is correct
54 Correct 416 ms 3052 KB Output is correct
55 Correct 468 ms 3564 KB Output is correct
56 Correct 401 ms 3052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 26 ms 2284 KB Output is correct
10 Correct 7 ms 876 KB Output is correct
11 Correct 16 ms 1516 KB Output is correct
12 Correct 19 ms 1772 KB Output is correct
13 Correct 23 ms 2028 KB Output is correct
14 Correct 16 ms 1388 KB Output is correct
15 Correct 4 ms 620 KB Output is correct
16 Correct 15 ms 1388 KB Output is correct
17 Correct 37 ms 3180 KB Output is correct
18 Correct 21 ms 1900 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 12 ms 1132 KB Output is correct
35 Correct 29 ms 2944 KB Output is correct
36 Correct 41 ms 3436 KB Output is correct
37 Correct 104 ms 3052 KB Output is correct
38 Correct 116 ms 3564 KB Output is correct
39 Correct 28 ms 2924 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 23 ms 1644 KB Output is correct
42 Correct 37 ms 3180 KB Output is correct
43 Correct 5 ms 620 KB Output is correct
44 Correct 23 ms 2176 KB Output is correct
45 Correct 32 ms 2412 KB Output is correct
46 Correct 39 ms 3436 KB Output is correct
47 Correct 12 ms 1388 KB Output is correct
48 Correct 2 ms 364 KB Output is correct
49 Correct 95 ms 3052 KB Output is correct
50 Correct 44 ms 3308 KB Output is correct
51 Correct 63 ms 2924 KB Output is correct
52 Correct 45 ms 3180 KB Output is correct
53 Correct 40 ms 3436 KB Output is correct
54 Correct 107 ms 3180 KB Output is correct
55 Correct 35 ms 3084 KB Output is correct
56 Correct 53 ms 3052 KB Output is correct
57 Correct 40 ms 2924 KB Output is correct
58 Correct 422 ms 3180 KB Output is correct
59 Correct 450 ms 3436 KB Output is correct
60 Correct 418 ms 3180 KB Output is correct
61 Correct 448 ms 3404 KB Output is correct
62 Correct 416 ms 3052 KB Output is correct
63 Correct 468 ms 3564 KB Output is correct
64 Correct 401 ms 3052 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Correct 487 ms 3308 KB Output is correct
68 Correct 485 ms 3180 KB Output is correct
69 Correct 420 ms 2796 KB Output is correct
70 Correct 449 ms 3332 KB Output is correct
71 Correct 453 ms 3052 KB Output is correct
72 Correct 438 ms 3180 KB Output is correct
73 Correct 446 ms 3052 KB Output is correct
74 Correct 481 ms 3292 KB Output is correct
75 Correct 454 ms 3052 KB Output is correct
76 Correct 489 ms 3308 KB Output is correct
77 Correct 96 ms 3564 KB Output is correct
78 Correct 46 ms 3180 KB Output is correct
79 Correct 70 ms 2924 KB Output is correct
80 Correct 76 ms 3052 KB Output is correct
81 Correct 68 ms 3052 KB Output is correct
82 Correct 62 ms 3308 KB Output is correct
83 Correct 52 ms 3052 KB Output is correct
84 Correct 298 ms 3436 KB Output is correct
85 Correct 508 ms 3692 KB Output is correct
86 Correct 1 ms 364 KB Output is correct
87 Correct 1 ms 364 KB Output is correct
88 Correct 516 ms 3564 KB Output is correct
89 Correct 102 ms 876 KB Output is correct
90 Correct 1 ms 364 KB Output is correct