Submission #155274

# Submission time Handle Problem Language Result Execution time Memory
155274 2019-09-27T12:42:27 Z Dasha_Gnedko Chessboard (IZhO18_chessboard) C++14
47 / 100
2000 ms 3312 KB
#include <bits/stdc++.h>

//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define endl '\n'
//#define int long long

using namespace std;

//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 100;
const int M = 21890;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const int P = 1336;
const ld eps = 0.000000001;
const int inf = 1e9 + 7;
const ll inff = 1e18 + 7;

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());

int l1[N], r1[N], l2[N], r2[N];
int m;
int lp[N];
vector <int> pr;
vector < pair <int, int> > ve;
vector <int> a;

void DFS(int i, ll ch)
{
//    cerr << i << " " << ch << endl;
    if (i == ve.size())
    {
        a.pb(ch);
        return;
    }
    for(int j = 0; j <= ve[i].S; j++)
    {
        if (j == 0) DFS(i + 1, ch);
        else
        {
            ch *= ve[i].F;
            DFS(i + 1, ch);
        }
    }
}

pair <int, pair <ll, ll> > get(ll r, ll x, ll y, int fl)
{
    ll gx = x / r, gy = y / r;
    if (x % r) gx++;
    if (y % r) gy++;

    bool f;

    if (fl == 0)
    {
        if (gx % 2)
        {
            if (gy % 2) f = 0;
            else f = 1;
        }
        else if (gy % 2) f = 1;
        else f = 0;
    }
    else
    {
        if (gx % 2)
        {
            if (gy % 2) f = 1;
            else f = 0;
        }
        else if (gy % 2) f = 0;
        else f = 1;
    }

    ll bx = 1ll * gx * r - x + 1;
    ll by = 1ll * gy * r - y + 1;

    return {f, {bx, by}};

}

ll solve(ll r, ll x1, ll y1, ll x2, ll y2, int fl)
{
    ll gx1 = x1 / r + 1, gx2 = x2 / r;
    if (x1 % r != 1) gx1++;
    if (x1 % r == 0) gx1--;

    ll gy1 = y1 / r + 1, gy2 = y2 / r;
    if (y1 % r != 1) gy1++;
    if (y1 % r == 0) gy1--;

    if (r == 1)
    {
        gx1 = x1; gx2 = x2; gy1 = y1; gy2 = y2;
    }


    if (gx1 > gx2 || gy1 > gy2)
    {

//    cout << "! " << gx1 << " " << gx2 << "     " << gy1 << " " << gy2 << endl;
        if (gx1 > gx2 && gy1 > gy2)
        {
            pair < int, pair <ll, ll> > p = get(r, x1, y1, fl);
//            cout << x1 << " " << y1 << " " << p.S.F << " " << p.S.S << endl;
            if (x1 + p.S.F > x2 && y1 + p.S.S > y2)
            {
//                cout << x1 << " " << y1 << " " << p.F << endl;
                if (p.F == 0) return 1ll * (x2 - x1 + 1) * (y2 - y1 + 1);
                else return 0;
            }

            if (x1 + p.S.F > x2)
            {
                ll S = 1ll * (x2 - x1 + 1) * p.S.S;
                if (p.F == 0) return S;
                else return 1ll * (x2 - x1 + 1) * (y2 - y1 + 1) - S;
            }

            if (y1 + p.S.S > y2)
            {
                ll S = 1ll * (y2 - y1 + 1) * p.S.F;
                if (p.F == 0) return S;
                else return 1ll * (x2 - x1 + 1) * (y2 - y1 + 1) - S;
            }

            ll v1 = 1ll * p.S.F * p.S.S + 1ll * (x2 - x1 + 1 - p.S.F) * (y2 - y1 + 1 - p.S.S);
            ll v2 = 1ll * (x2 - x1 + 1) * (y2 - y1 + 1) - v1;

//            cout << "!!! " << v1 << " " << v2 << endl;

            if (p.F == 0) return v1;
            else return v2;

        }

        return 0;


    }

    ll stx = 1ll * gx1 * r - r + 1;
    ll ex = 1ll * gx2 * r;

    ll sty = 1ll * gy1 * r - r + 1;
    ll ey = 1ll * gy2 * r;

    ll kolx = (gx2 - gx1 + 1 + 1) / 2;
    ll koly = (gy2 - gy1 + 1 + 1) / 2;

    ll S = 1ll * kolx * koly * r * r;

    kolx = (gx2 - gx1 + 1) / 2;
    koly = (gy2 - gy1 + 1) / 2;

    S += 1ll * kolx * koly * r * r;

    pair < int, pair <ll, ll> > p = get(r, stx, sty, fl);

//    cout << p.F << " " << S << endl;

    if (p.F == 0) return S;
    else return 1ll * (ex - stx + 1) * (ey - sty + 1) - S;

}

ll get_ans(ll r, int n)
{
    ll S = 1ll * n * n;
//    cout << S << endl;

    n /= r;

    ll kx = 1ll * (n + 1) / 2;
//    cerr << kx << " " << kx * kx << endl;
    ll k1 = kx * kx;
    kx = 1ll * n / 2;
    k1 += kx * kx;
    k1 *= r * r;

    ll k2 = S - k1;


    ll v1 = 0, v2 = 0;

    for(int i = 0; i < m; i++)
    {
        ll pl = 1ll * (l2[i] - l1[i] + 1) * (r2[i] - r1[i] + 1);
        ll c1 = solve(r, l1[i], r1[i], l2[i], r2[i], 1);
        ll c2 = solve(r, l1[i], r1[i], l2[i], r2[i], 0);

//        cout << r << " " << solve(r, l1[i], r1[i], l2[i], r2[i], 1) << endl;
        v1 += c1;
        k1 -= (pl - c1);

        v2 += c2;
        k2 -= (pl - c2);
    }

//    cout << v1 << " " << v2 <<

//    cout << r << " " << v1 << " " << k1 << " " << v2 << " " << k2 << endl;

    return min(v1 + k1, v2 + k2);

}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        cin >> l1[i] >> r1[i] >> l2[i] >> r2[i];
//        cout << solve(2, l1[i], r1[i], l2[i], r2[i], 1) << endl;
    }

//    cout << get_ans(2, n);
//
//    return 0;

    for(int i = 2; i < N; i++)
    {
        if (lp[i] == 0)
        {
            lp[i] = i;
            pr.pb(i);
        }
        for(int j = 0; j < pr.size() && pr[j] * i < N && pr[j] <= lp[i]; j++)
        {
            lp[pr[j] * i] = pr[j];
        }
    }

    if (lp[n] == n)
    {
        cout << get_ans(1, n);
        return 0;
    }

    int ch = n, kopn = n;

    while (ch > 1)
    {
//        cerr << ch << endl;3
        int d = lp[ch];
        if (ve.size() && ve.back().F == d) ve.back().S++;
        else ve.pb({d, 1});
        ch /= d;
    }

//    return 0;

    DFS(0, 1);

    ll ans = inff;

    for(auto to: a)
    {
//        cout << to << endl;
        if (to == kopn) continue;
//        cout << get_ans(to, kopn) << endl;
        ans = min(ans, get_ans(to, kopn));
    }

    cout << ans;


}

Compilation message

chessboard.cpp: In function 'void DFS(int, long long int)':
chessboard.cpp:44:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (i == ve.size())
         ~~^~~~~~~~~~~~
chessboard.cpp: In function 'int32_t main()':
chessboard.cpp:252:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < pr.size() && pr[j] * i < N && pr[j] <= lp[i]; j++)
                        ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 888 KB Output is correct
2 Correct 3 ms 812 KB Output is correct
3 Correct 3 ms 764 KB Output is correct
4 Correct 3 ms 888 KB Output is correct
5 Correct 3 ms 888 KB Output is correct
6 Correct 3 ms 888 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2680 KB Output is correct
2 Correct 12 ms 1528 KB Output is correct
3 Correct 26 ms 2164 KB Output is correct
4 Correct 28 ms 2296 KB Output is correct
5 Correct 35 ms 2424 KB Output is correct
6 Correct 24 ms 1656 KB Output is correct
7 Correct 8 ms 1144 KB Output is correct
8 Correct 23 ms 2168 KB Output is correct
9 Correct 55 ms 3112 KB Output is correct
10 Correct 35 ms 2488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 888 KB Output is correct
2 Correct 4 ms 888 KB Output is correct
3 Correct 5 ms 888 KB Output is correct
4 Correct 5 ms 888 KB Output is correct
5 Correct 4 ms 888 KB Output is correct
6 Correct 4 ms 888 KB Output is correct
7 Correct 4 ms 860 KB Output is correct
8 Correct 4 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 3 ms 892 KB Output is correct
11 Correct 4 ms 888 KB Output is correct
12 Correct 3 ms 888 KB Output is correct
13 Correct 4 ms 892 KB Output is correct
14 Correct 4 ms 888 KB Output is correct
15 Correct 4 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 888 KB Output is correct
2 Correct 4 ms 888 KB Output is correct
3 Correct 5 ms 888 KB Output is correct
4 Correct 5 ms 888 KB Output is correct
5 Correct 4 ms 888 KB Output is correct
6 Correct 4 ms 888 KB Output is correct
7 Correct 4 ms 860 KB Output is correct
8 Correct 4 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 3 ms 892 KB Output is correct
11 Correct 4 ms 888 KB Output is correct
12 Correct 3 ms 888 KB Output is correct
13 Correct 4 ms 892 KB Output is correct
14 Correct 4 ms 888 KB Output is correct
15 Correct 4 ms 860 KB Output is correct
16 Correct 28 ms 1756 KB Output is correct
17 Correct 47 ms 2984 KB Output is correct
18 Correct 93 ms 3164 KB Output is correct
19 Correct 470 ms 2936 KB Output is correct
20 Correct 520 ms 3312 KB Output is correct
21 Correct 44 ms 2936 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 71 ms 2280 KB Output is correct
24 Correct 82 ms 3064 KB Output is correct
25 Correct 17 ms 1144 KB Output is correct
26 Correct 52 ms 2552 KB Output is correct
27 Correct 81 ms 2680 KB Output is correct
28 Correct 88 ms 3164 KB Output is correct
29 Correct 20 ms 1912 KB Output is correct
30 Correct 6 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2680 KB Output is correct
2 Correct 12 ms 1528 KB Output is correct
3 Correct 26 ms 2164 KB Output is correct
4 Correct 28 ms 2296 KB Output is correct
5 Correct 35 ms 2424 KB Output is correct
6 Correct 24 ms 1656 KB Output is correct
7 Correct 8 ms 1144 KB Output is correct
8 Correct 23 ms 2168 KB Output is correct
9 Correct 55 ms 3112 KB Output is correct
10 Correct 35 ms 2488 KB Output is correct
11 Correct 4 ms 888 KB Output is correct
12 Correct 4 ms 888 KB Output is correct
13 Correct 5 ms 888 KB Output is correct
14 Correct 5 ms 888 KB Output is correct
15 Correct 4 ms 888 KB Output is correct
16 Correct 4 ms 888 KB Output is correct
17 Correct 4 ms 860 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 3 ms 888 KB Output is correct
20 Correct 3 ms 892 KB Output is correct
21 Correct 4 ms 888 KB Output is correct
22 Correct 3 ms 888 KB Output is correct
23 Correct 4 ms 892 KB Output is correct
24 Correct 4 ms 888 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 28 ms 1756 KB Output is correct
27 Correct 47 ms 2984 KB Output is correct
28 Correct 93 ms 3164 KB Output is correct
29 Correct 470 ms 2936 KB Output is correct
30 Correct 520 ms 3312 KB Output is correct
31 Correct 44 ms 2936 KB Output is correct
32 Correct 5 ms 888 KB Output is correct
33 Correct 71 ms 2280 KB Output is correct
34 Correct 82 ms 3064 KB Output is correct
35 Correct 17 ms 1144 KB Output is correct
36 Correct 52 ms 2552 KB Output is correct
37 Correct 81 ms 2680 KB Output is correct
38 Correct 88 ms 3164 KB Output is correct
39 Correct 20 ms 1912 KB Output is correct
40 Correct 6 ms 1016 KB Output is correct
41 Correct 394 ms 2968 KB Output is correct
42 Correct 92 ms 3064 KB Output is correct
43 Correct 198 ms 2964 KB Output is correct
44 Correct 88 ms 3164 KB Output is correct
45 Correct 59 ms 3192 KB Output is correct
46 Correct 419 ms 3192 KB Output is correct
47 Correct 51 ms 2936 KB Output is correct
48 Correct 144 ms 3064 KB Output is correct
49 Correct 81 ms 2940 KB Output is correct
50 Execution timed out 2037 ms 3000 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 888 KB Output is correct
2 Correct 3 ms 812 KB Output is correct
3 Correct 3 ms 764 KB Output is correct
4 Correct 3 ms 888 KB Output is correct
5 Correct 3 ms 888 KB Output is correct
6 Correct 3 ms 888 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 39 ms 2680 KB Output is correct
10 Correct 12 ms 1528 KB Output is correct
11 Correct 26 ms 2164 KB Output is correct
12 Correct 28 ms 2296 KB Output is correct
13 Correct 35 ms 2424 KB Output is correct
14 Correct 24 ms 1656 KB Output is correct
15 Correct 8 ms 1144 KB Output is correct
16 Correct 23 ms 2168 KB Output is correct
17 Correct 55 ms 3112 KB Output is correct
18 Correct 35 ms 2488 KB Output is correct
19 Correct 4 ms 888 KB Output is correct
20 Correct 4 ms 888 KB Output is correct
21 Correct 5 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 4 ms 888 KB Output is correct
24 Correct 4 ms 888 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 4 ms 888 KB Output is correct
27 Correct 3 ms 888 KB Output is correct
28 Correct 3 ms 892 KB Output is correct
29 Correct 4 ms 888 KB Output is correct
30 Correct 3 ms 888 KB Output is correct
31 Correct 4 ms 892 KB Output is correct
32 Correct 4 ms 888 KB Output is correct
33 Correct 4 ms 860 KB Output is correct
34 Correct 28 ms 1756 KB Output is correct
35 Correct 47 ms 2984 KB Output is correct
36 Correct 93 ms 3164 KB Output is correct
37 Correct 470 ms 2936 KB Output is correct
38 Correct 520 ms 3312 KB Output is correct
39 Correct 44 ms 2936 KB Output is correct
40 Correct 5 ms 888 KB Output is correct
41 Correct 71 ms 2280 KB Output is correct
42 Correct 82 ms 3064 KB Output is correct
43 Correct 17 ms 1144 KB Output is correct
44 Correct 52 ms 2552 KB Output is correct
45 Correct 81 ms 2680 KB Output is correct
46 Correct 88 ms 3164 KB Output is correct
47 Correct 20 ms 1912 KB Output is correct
48 Correct 6 ms 1016 KB Output is correct
49 Correct 394 ms 2968 KB Output is correct
50 Correct 92 ms 3064 KB Output is correct
51 Correct 198 ms 2964 KB Output is correct
52 Correct 88 ms 3164 KB Output is correct
53 Correct 59 ms 3192 KB Output is correct
54 Correct 419 ms 3192 KB Output is correct
55 Correct 51 ms 2936 KB Output is correct
56 Correct 144 ms 3064 KB Output is correct
57 Correct 81 ms 2940 KB Output is correct
58 Execution timed out 2037 ms 3000 KB Time limit exceeded
59 Halted 0 ms 0 KB -