답안 #747120

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747120 2023-05-23T16:11:37 Z heeheeheehaaw Chessboard (IZhO18_chessboard) C++17
100 / 100
482 ms 5820 KB
#include <iostream>
#include <vector>
#define int long long

using namespace std;

int a[1005][1005];
int spw[1005][1005], spb[1005][1005];
int x1[100005], x2[100005], y1[100005], y2[100005];

void add(int x, int y, int val, int &s1, int &s2)
{
    if((int)(x + y) % 2 == 0)
        s1 += val;
    else
        s2 += val;
    return;
}

signed main()
{
    int s1 = 0, s2 = 0;
    int n, k;
    cin>>n>>k;

    vector<int> divs;
    for(int i = 1; i * i <= n; i++)
        if(n % i == 0)
        {
            divs.push_back(i);
            if(i * i != n && i != 1)
                divs.push_back(n / i);
        }

    for(int i = 1; i <= k; i++)
        cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];

    int minn = n * n;
    for(auto d : divs)
    {
        int s1 = 0, s2 = 0;
        for(int i = 1; i <= k; i++)
        {
            int cx1 = (int)(x1[i] - 1) / d + 1;
            int cy1 = (int)(y1[i] - 1) / d + 1;
            int cx2 = (int)(x2[i] - 1) / d + 1;
            int cy2 = (int)(y2[i] - 1) / d + 1;

            if(cx1 == cx2 && cy1 == cy2)
            {
                int tt = (x2[i] - x1[i] + 1) * (y2[i] - y1[i] + 1);
                add(cx1, cy1, tt, s1, s2);
            }
            else if(cx1 == cx2 && cy1 != cy2)
            {
                /// colt stanga
                int tt = ((d * cy1) - y1[i] + 1) * (x2[i] - x1[i] + 1);
                add(cx1, cy1, tt, s1, s2);
                /// colt dreapta
                    tt = (y2[i] - d * (cy2 - 1)) * (x2[i] - x1[i] + 1);
                add(cx2, cy2, tt, s1, s2);
                /// mijloc
                if(cy2 - cy1 - 1 >= 1)
                {
                    int tt = d * (x2[i] - x1[i] + 1);
                    int np = cy2 - cy1 - 1;
                    int nri = np / 2, nrp = np / 2;
                    if(np % 2 == 1)
                        nri++;
                    add(cx1, cy1 + 1, tt * nri, s1, s2);
                    add(cx1, cy1 + 2, tt * nrp, s1, s2);
                }
            }
            else if(cx1 != cx2 && cy1 == cy2)
            {
                /// colt sus
                int tt = ((d * cx1) - x1[i] + 1) * (y2[i] - y1[i] + 1);
                add(cx1, cy1, tt, s1, s2);
                /// colt jos
                    tt = (x2[i] - d * (cx2 - 1)) * (y2[i] - y1[i] + 1);
                add(cx2, cy2, tt, s1, s2);
                /// mijloc
                if(cx2 - cx1 - 1 >= 1)
                {
                    int tt = d * (y2[i] - y1[i] + 1);
                    int np = cx2 - cx1 - 1;
                    int nri = np / 2, nrp = np / 2;
                    if(np % 2 == 1)
                        nri++;
                    add(cx1 + 1, cy1, tt * nri, s1, s2);
                    add(cx1 + 2, cy1, tt * nrp, s1, s2);
                }
            }
            else
            {
                /// colt st sus
                int tt = (int)(d * cx1 - x1[i] + 1) * (d * cy1 - y1[i] + 1);
                add(cx1, cy1, tt, s1, s2);
                /// colt dr sus
                    tt = (int)(d * cx1 - x1[i] + 1) * (y2[i] - d * (cy2 - 1));
                add(cx1, cy2, tt, s1, s2);
                /// colt st jos
                    tt = (int)(x2[i] - d * (cx2 - 1)) * (d * cy1 - y1[i] + 1);
                add(cx2, cy1, tt, s1, s2);
                /// colt dr jos
                    tt = (int)(x2[i] - d * (cx2 - 1)) * (y2[i] - d * (cy2 - 1));
                add(cx2, cy2, tt, s1, s2);
                /// marg sus si jos
                if((int)cy2 - cy1 - 1 >= 1)
                {
                    tt = (int)(d * cx1 - x1[i] + 1) * d;
                    int np = (int)cy2 - cy1 - 1;
                    int nri = (int)np / 2, nrp = np / 2;
                    if((int)np % 2 == 1)
                        nri = (int)nri + 1;
                    add(cx1, (int)cy1 + 1, (int)tt * nri, s1, s2);
                    add(cx1, (int)cy1 + 2, (int)tt * nrp, s1, s2);
                    tt = (int)(x2[i] - d * (cx2 - 1)) * d;
                    add(cx2, (int)cy1 + 1, (int)tt * nri, s1, s2);
                    add(cx2, (int)cy1 + 2, (int)tt * nrp, s1, s2);
                }
                /// marg st si dr
                if((int)cx2 - cx1 - 1 >= 1)
                {
                    tt = (int)(d * cy1 - y1[i] + 1) * d;
                    int np = (int)cx2 - cx1 - 1;
                    int nri = (int)np / 2, nrp = (int)np / 2;
                    if((int)np % 2 == 1)
                        nri = (int)nri + 1;
                    add((int)cx1 + 1, cy1, (int)tt * nri, s1, s2);
                    add((int)cx1 + 2, cy1, (int)tt * nrp, s1, s2);
                    tt = (int)(y2[i] - d * (cy2 - 1)) * d;
                    add((int)cx1 + 1, cy2, (int)tt * nri, s1, s2);
                    add((int)cx1 + 2, cy2, (int)tt * nrp, s1, s2);
                }

                /// mijloc
                if((int)(cx2 - cx1 - 1) >= 1 && (int)(cy2 - cy1 - 1) >= 1)
                {
                    int nrp = (int)(cx2 - cx1 - 1) * (cy2 - cy1 - 1);
                    int d1 = (int)nrp / 2, d2 = (int)nrp / 2;
                    if(nrp % 2 == 1)
                        d1++;
                    add(cx1 + 1, cy1 + 1, (int)d * d * d1, s1, s2);
                    add(cx1 + 1, cy1 + 2, (int)d * d * d2, s1, s2);
                }
            }
        }

        int d1 = (int)n * n / d / d / 2, d2 = (int)n * n / d / d / 2;
        if((n * n / d / d) % 2 == 1) d1++;
        d2 = (int)n * n / d / d - d1;
        d1 = (int)d1 * d * d;
        d2 = (int)d2 * d * d;
        int ans = min((int)d1 - s1 + s2, (int)d2 - s2 + s1);
        minn = min(minn, ans);
    }

    cout<<minn<<'\n';

    return 0;
}

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:22:9: warning: unused variable 's1' [-Wunused-variable]
   22 |     int s1 = 0, s2 = 0;
      |         ^~
chessboard.cpp:22:17: warning: unused variable 's2' [-Wunused-variable]
   22 |     int s1 = 0, s2 = 0;
      |                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 3712 KB Output is correct
2 Correct 18 ms 1108 KB Output is correct
3 Correct 52 ms 2464 KB Output is correct
4 Correct 41 ms 2504 KB Output is correct
5 Correct 60 ms 3280 KB Output is correct
6 Correct 39 ms 2204 KB Output is correct
7 Correct 9 ms 724 KB Output is correct
8 Correct 40 ms 2224 KB Output is correct
9 Correct 122 ms 5184 KB Output is correct
10 Correct 61 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 30 ms 1516 KB Output is correct
17 Correct 75 ms 4264 KB Output is correct
18 Correct 83 ms 5000 KB Output is correct
19 Correct 121 ms 4508 KB Output is correct
20 Correct 143 ms 4996 KB Output is correct
21 Correct 74 ms 4044 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 45 ms 2260 KB Output is correct
24 Correct 101 ms 4480 KB Output is correct
25 Correct 9 ms 724 KB Output is correct
26 Correct 55 ms 2952 KB Output is correct
27 Correct 80 ms 3508 KB Output is correct
28 Correct 89 ms 4500 KB Output is correct
29 Correct 29 ms 1868 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 3712 KB Output is correct
2 Correct 18 ms 1108 KB Output is correct
3 Correct 52 ms 2464 KB Output is correct
4 Correct 41 ms 2504 KB Output is correct
5 Correct 60 ms 3280 KB Output is correct
6 Correct 39 ms 2204 KB Output is correct
7 Correct 9 ms 724 KB Output is correct
8 Correct 40 ms 2224 KB Output is correct
9 Correct 122 ms 5184 KB Output is correct
10 Correct 61 ms 3020 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 30 ms 1516 KB Output is correct
27 Correct 75 ms 4264 KB Output is correct
28 Correct 83 ms 5000 KB Output is correct
29 Correct 121 ms 4508 KB Output is correct
30 Correct 143 ms 4996 KB Output is correct
31 Correct 74 ms 4044 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 45 ms 2260 KB Output is correct
34 Correct 101 ms 4480 KB Output is correct
35 Correct 9 ms 724 KB Output is correct
36 Correct 55 ms 2952 KB Output is correct
37 Correct 80 ms 3508 KB Output is correct
38 Correct 89 ms 4500 KB Output is correct
39 Correct 29 ms 1868 KB Output is correct
40 Correct 3 ms 340 KB Output is correct
41 Correct 134 ms 4420 KB Output is correct
42 Correct 131 ms 4480 KB Output is correct
43 Correct 114 ms 4188 KB Output is correct
44 Correct 104 ms 4428 KB Output is correct
45 Correct 131 ms 4652 KB Output is correct
46 Correct 179 ms 4524 KB Output is correct
47 Correct 97 ms 4228 KB Output is correct
48 Correct 106 ms 4300 KB Output is correct
49 Correct 100 ms 4180 KB Output is correct
50 Correct 395 ms 4432 KB Output is correct
51 Correct 388 ms 4620 KB Output is correct
52 Correct 375 ms 4404 KB Output is correct
53 Correct 410 ms 4540 KB Output is correct
54 Correct 368 ms 4112 KB Output is correct
55 Correct 389 ms 4044 KB Output is correct
56 Correct 348 ms 3640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 70 ms 3712 KB Output is correct
10 Correct 18 ms 1108 KB Output is correct
11 Correct 52 ms 2464 KB Output is correct
12 Correct 41 ms 2504 KB Output is correct
13 Correct 60 ms 3280 KB Output is correct
14 Correct 39 ms 2204 KB Output is correct
15 Correct 9 ms 724 KB Output is correct
16 Correct 40 ms 2224 KB Output is correct
17 Correct 122 ms 5184 KB Output is correct
18 Correct 61 ms 3020 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 324 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 30 ms 1516 KB Output is correct
35 Correct 75 ms 4264 KB Output is correct
36 Correct 83 ms 5000 KB Output is correct
37 Correct 121 ms 4508 KB Output is correct
38 Correct 143 ms 4996 KB Output is correct
39 Correct 74 ms 4044 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 45 ms 2260 KB Output is correct
42 Correct 101 ms 4480 KB Output is correct
43 Correct 9 ms 724 KB Output is correct
44 Correct 55 ms 2952 KB Output is correct
45 Correct 80 ms 3508 KB Output is correct
46 Correct 89 ms 4500 KB Output is correct
47 Correct 29 ms 1868 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 134 ms 4420 KB Output is correct
50 Correct 131 ms 4480 KB Output is correct
51 Correct 114 ms 4188 KB Output is correct
52 Correct 104 ms 4428 KB Output is correct
53 Correct 131 ms 4652 KB Output is correct
54 Correct 179 ms 4524 KB Output is correct
55 Correct 97 ms 4228 KB Output is correct
56 Correct 106 ms 4300 KB Output is correct
57 Correct 100 ms 4180 KB Output is correct
58 Correct 395 ms 4432 KB Output is correct
59 Correct 388 ms 4620 KB Output is correct
60 Correct 375 ms 4404 KB Output is correct
61 Correct 410 ms 4540 KB Output is correct
62 Correct 368 ms 4112 KB Output is correct
63 Correct 389 ms 4044 KB Output is correct
64 Correct 348 ms 3640 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 482 ms 3868 KB Output is correct
68 Correct 443 ms 3908 KB Output is correct
69 Correct 384 ms 3100 KB Output is correct
70 Correct 392 ms 3236 KB Output is correct
71 Correct 424 ms 3080 KB Output is correct
72 Correct 387 ms 2940 KB Output is correct
73 Correct 388 ms 3048 KB Output is correct
74 Correct 414 ms 3204 KB Output is correct
75 Correct 401 ms 3048 KB Output is correct
76 Correct 443 ms 5512 KB Output is correct
77 Correct 151 ms 5740 KB Output is correct
78 Correct 104 ms 5240 KB Output is correct
79 Correct 121 ms 4976 KB Output is correct
80 Correct 123 ms 5196 KB Output is correct
81 Correct 127 ms 4900 KB Output is correct
82 Correct 124 ms 5448 KB Output is correct
83 Correct 106 ms 4996 KB Output is correct
84 Correct 282 ms 5636 KB Output is correct
85 Correct 461 ms 5732 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 476 ms 5820 KB Output is correct
89 Correct 92 ms 1232 KB Output is correct
90 Correct 1 ms 340 KB Output is correct