Submission #172844

# Submission time Handle Problem Language Result Execution time Memory
172844 2020-01-02T16:43:17 Z muhammad_hokimiyon Chessboard (IZhO18_chessboard) C++17
100 / 100
1635 ms 6344 KB
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long

using namespace std;

const int N = 1e5 + 7;
const int M = 23;
const int mod = 998244353;

ll n,k;
int A[N];
int B[N];
int C[N];
int D[N];
ll x1, y2, a, b;

ll f( int x , int y , int div )
{
    x1 = y / div;
    y2 = x / div;
    x1 = x1 * y2 + 1;
    x1 /= 2;
    x1 *= div * 1ll * div;
    a = y % div;
    b = x % div;
    if( ((y - 1) / div % 2 == 0 && div > 1) ){
        x1 += ((x / div + 1) / 2) * 1ll * div * a;
    }else{
        x1 += x / div / 2 * 1ll * div * a;
    }
    if( ((x - 1) / div % 2 == 0) ){
        x1 += (y / div + 1) / 2 * 1ll * div * b;
    }else{
        x1 += y / div / 2 * 1ll * div * b;
    }
    if( ((x - 1) / div % 2 == 1 && (y - 1) / div % 2 == 1) || ((x - 1) / div % 2 == 0 && (y - 1) / div % 2 == 0) ){
        x1 += (x % div) * 1ll * (y % div);
    }
    //cout << x << " " << y << " " << div << " " << x1 << " " << fg[x][y] << "\n";
    return x1;
}

void solve1()
{
    cin >> n >> k;
    vector < ll > div;
    for( int i = 1; i < n; i++ ){
        if( n % i == 0 )div.push_back(i);
    }
    for( int i = 0; i < k; i++ ){
        cin >> A[i] >> B[i] >> C[i] >> D[i];
    }
    ll ans = 1e14;
    for( int i = 0; i < (int)div.size(); i++ ){
        ll nn = n / div[i];
        ll fg = div[i] * div[i];
        ll B1 = (nn + 1) / 2;
        ll B2 = nn / 2;
        B1 *= fg;
        B2 *= fg;
        B1 *= (nn + 1) / 2;
        B2 *= nn / 2;
        B1 += B2;
        ll B3 = (nn) / 2;
        ll B4 = (nn + 1) / 2;
        B3 *= fg;
        B4 *= fg;
        B3 *= (nn + 1) / 2;
        B4 *= (nn) / 2;
        B3 += B4;
        vector < ll > w(k , 0);
        vector < ll > b(k , 0);
        ll x, y;
        for( int j = 0; j < k; j++ ){
            x = f(C[j] , D[j] , div[i]) - f(C[j] , B[j] - 1 , div[i]) -
            f(A[j] - 1, D[j] , div[i]) + f(A[j] - 1 , B[j] - 1 , div[i]);
            y = (C[j] - A[j] + 1) * 1ll * (D[j] - B[j] + 1);
            w[j] = y - x;
            b[j] = x;
        }
        x = 0;
        y = 0;
        //cout << B1 << " " << B3 << "\n";
        for( int j = 0; j < k; j++ ){
            B1 -= b[j];
            x += w[j];
            y += b[j];
            B3 -= w[j];
            //if( div[i] == 1 )cout << b[j] << " ";
        }
        //cout << "\n";
        //cout << B1 + x << " " << B3 + y << " " << div[i] << "\n";
        ans = min(ans , B1 + x);
        ans = min(ans , B3 + y);
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    int cghf = 1;//cin >> cghf;
    while( cghf-- ){
        solve1();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3832 KB Output is correct
2 Correct 12 ms 1368 KB Output is correct
3 Correct 24 ms 2552 KB Output is correct
4 Correct 26 ms 2652 KB Output is correct
5 Correct 35 ms 3320 KB Output is correct
6 Correct 22 ms 2220 KB Output is correct
7 Correct 7 ms 760 KB Output is correct
8 Correct 22 ms 2296 KB Output is correct
9 Correct 52 ms 4600 KB Output is correct
10 Correct 30 ms 3036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 22 ms 1680 KB Output is correct
17 Correct 42 ms 3184 KB Output is correct
18 Correct 71 ms 4828 KB Output is correct
19 Correct 327 ms 4600 KB Output is correct
20 Correct 368 ms 5248 KB Output is correct
21 Correct 40 ms 4088 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 54 ms 2488 KB Output is correct
24 Correct 68 ms 4472 KB Output is correct
25 Correct 13 ms 760 KB Output is correct
26 Correct 42 ms 2916 KB Output is correct
27 Correct 68 ms 3196 KB Output is correct
28 Correct 71 ms 4624 KB Output is correct
29 Correct 18 ms 1912 KB Output is correct
30 Correct 0 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3832 KB Output is correct
2 Correct 12 ms 1368 KB Output is correct
3 Correct 24 ms 2552 KB Output is correct
4 Correct 26 ms 2652 KB Output is correct
5 Correct 35 ms 3320 KB Output is correct
6 Correct 22 ms 2220 KB Output is correct
7 Correct 7 ms 760 KB Output is correct
8 Correct 22 ms 2296 KB Output is correct
9 Correct 52 ms 4600 KB Output is correct
10 Correct 30 ms 3036 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 380 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 22 ms 1680 KB Output is correct
27 Correct 42 ms 3184 KB Output is correct
28 Correct 71 ms 4828 KB Output is correct
29 Correct 327 ms 4600 KB Output is correct
30 Correct 368 ms 5248 KB Output is correct
31 Correct 40 ms 4088 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 54 ms 2488 KB Output is correct
34 Correct 68 ms 4472 KB Output is correct
35 Correct 13 ms 760 KB Output is correct
36 Correct 42 ms 2916 KB Output is correct
37 Correct 68 ms 3196 KB Output is correct
38 Correct 71 ms 4624 KB Output is correct
39 Correct 18 ms 1912 KB Output is correct
40 Correct 0 ms 504 KB Output is correct
41 Correct 270 ms 4272 KB Output is correct
42 Correct 77 ms 5112 KB Output is correct
43 Correct 151 ms 4800 KB Output is correct
44 Correct 76 ms 5284 KB Output is correct
45 Correct 57 ms 5624 KB Output is correct
46 Correct 303 ms 5128 KB Output is correct
47 Correct 49 ms 4856 KB Output is correct
48 Correct 114 ms 4808 KB Output is correct
49 Correct 71 ms 4728 KB Output is correct
50 Correct 1433 ms 5168 KB Output is correct
51 Correct 1533 ms 5372 KB Output is correct
52 Correct 1451 ms 5096 KB Output is correct
53 Correct 1573 ms 5248 KB Output is correct
54 Correct 1412 ms 5240 KB Output is correct
55 Correct 1620 ms 5480 KB Output is correct
56 Correct 1369 ms 4968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 420 KB Output is correct
9 Correct 37 ms 3832 KB Output is correct
10 Correct 12 ms 1368 KB Output is correct
11 Correct 24 ms 2552 KB Output is correct
12 Correct 26 ms 2652 KB Output is correct
13 Correct 35 ms 3320 KB Output is correct
14 Correct 22 ms 2220 KB Output is correct
15 Correct 7 ms 760 KB Output is correct
16 Correct 22 ms 2296 KB Output is correct
17 Correct 52 ms 4600 KB Output is correct
18 Correct 30 ms 3036 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 22 ms 1680 KB Output is correct
35 Correct 42 ms 3184 KB Output is correct
36 Correct 71 ms 4828 KB Output is correct
37 Correct 327 ms 4600 KB Output is correct
38 Correct 368 ms 5248 KB Output is correct
39 Correct 40 ms 4088 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 54 ms 2488 KB Output is correct
42 Correct 68 ms 4472 KB Output is correct
43 Correct 13 ms 760 KB Output is correct
44 Correct 42 ms 2916 KB Output is correct
45 Correct 68 ms 3196 KB Output is correct
46 Correct 71 ms 4624 KB Output is correct
47 Correct 18 ms 1912 KB Output is correct
48 Correct 0 ms 504 KB Output is correct
49 Correct 270 ms 4272 KB Output is correct
50 Correct 77 ms 5112 KB Output is correct
51 Correct 151 ms 4800 KB Output is correct
52 Correct 76 ms 5284 KB Output is correct
53 Correct 57 ms 5624 KB Output is correct
54 Correct 303 ms 5128 KB Output is correct
55 Correct 49 ms 4856 KB Output is correct
56 Correct 114 ms 4808 KB Output is correct
57 Correct 71 ms 4728 KB Output is correct
58 Correct 1433 ms 5168 KB Output is correct
59 Correct 1533 ms 5372 KB Output is correct
60 Correct 1451 ms 5096 KB Output is correct
61 Correct 1573 ms 5248 KB Output is correct
62 Correct 1412 ms 5240 KB Output is correct
63 Correct 1620 ms 5480 KB Output is correct
64 Correct 1369 ms 4968 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 KB Output is correct
67 Correct 1540 ms 5212 KB Output is correct
68 Correct 1511 ms 5176 KB Output is correct
69 Correct 1294 ms 4784 KB Output is correct
70 Correct 1416 ms 5008 KB Output is correct
71 Correct 1417 ms 4948 KB Output is correct
72 Correct 1367 ms 4940 KB Output is correct
73 Correct 1400 ms 4880 KB Output is correct
74 Correct 1498 ms 5080 KB Output is correct
75 Correct 1396 ms 4784 KB Output is correct
76 Correct 1549 ms 5088 KB Output is correct
77 Correct 227 ms 5124 KB Output is correct
78 Correct 79 ms 3908 KB Output is correct
79 Correct 158 ms 3772 KB Output is correct
80 Correct 167 ms 3924 KB Output is correct
81 Correct 150 ms 3768 KB Output is correct
82 Correct 125 ms 3984 KB Output is correct
83 Correct 92 ms 3704 KB Output is correct
84 Correct 889 ms 4220 KB Output is correct
85 Correct 1635 ms 4432 KB Output is correct
86 Correct 4 ms 376 KB Output is correct
87 Correct 3 ms 380 KB Output is correct
88 Correct 1596 ms 6344 KB Output is correct
89 Correct 291 ms 1484 KB Output is correct
90 Correct 3 ms 504 KB Output is correct