Submission #346926

# Submission time Handle Problem Language Result Execution time Memory
346926 2021-01-11T10:52:05 Z blue Strange Device (APIO19_strange_device) C++17
100 / 100
1815 ms 26320 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long long_gcd(long long a, long long b)
{
    // cout << "gcd " << a << ' ' << b << '\n';
    if(a < b) swap(a, b);
    if(b == 0) return a;
    return long_gcd(a%b, b);
}

long long long_abs(long long x)
{
    if(x >= 0) return x;
    return -x;
}

long long long_sign(long long x)
{
    if(x > 0) return 1;
    return -1;
}

int main()
{
    int n;
    long long A, B;
    cin >> n >> A >> B;



    long long period = A / long_gcd(A, B+1);

    if (period > 2e18 / B) { // period * B > 2e18
        period = 1'000'000'000'000'000'000LL;
    } else {
        period *= B;
    }

    // cout << period << '\n';

    long long l, r;

    long long maxSize = 0;

    vector<long long> endpoints;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;
        maxSize = max(maxSize, r-l+1);

        if(l % period <= r % period)
        {
            endpoints.push_back(l % period + 1);
            endpoints.push_back(-(r % period) - 1 - 1);
        }
        else
        {
            endpoints.push_back(l % period + 1);
            endpoints.push_back(-period - 1);

            endpoints.push_back(0 + 1);
            endpoints.push_back(-(r % period) - 1 - 1);
        }
    }

    if(maxSize >= period)
    {
        cout << period << '\n';
        return 0;
    }


        // for(long long e: endpoints) cout << e << ' ';
        // cout << '\n';


    sort(endpoints.begin(), endpoints.end(), [] (long long p, long long q)
    {
        return long_abs(p) < long_abs(q);
    });

    // for(long long e: endpoints) cout << e << ' ';
    // cout << '\n';


    long long res = 0;

    long long count = 0;
    count += long_sign(endpoints[0]);
    for(int i = 1; i < endpoints.size(); i++)
    {
        // cout << count << ' ' << long_abs(endpoints[i]) - long_abs(endpoints[i-1]) << '\n';
        if(count > 0) res += long_abs(endpoints[i]) - long_abs(endpoints[i-1]);
        count += long_sign(endpoints[i]);
    }

    cout << res << '\n';
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:94:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int i = 1; i < endpoints.size(); i++)
      |                    ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 16 ms 748 KB Output is correct
3 Correct 18 ms 748 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 0 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 16 ms 748 KB Output is correct
17 Correct 170 ms 2528 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 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 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1114 ms 16840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1600 ms 16876 KB Output is correct
3 Correct 1674 ms 17012 KB Output is correct
4 Correct 1623 ms 17060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1600 ms 16876 KB Output is correct
3 Correct 1674 ms 17012 KB Output is correct
4 Correct 1623 ms 17060 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1668 ms 17024 KB Output is correct
7 Correct 1646 ms 16880 KB Output is correct
8 Correct 1670 ms 16840 KB Output is correct
9 Correct 1771 ms 16940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1600 ms 16876 KB Output is correct
3 Correct 1674 ms 17012 KB Output is correct
4 Correct 1623 ms 17060 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 159 ms 2520 KB Output is correct
7 Correct 165 ms 2528 KB Output is correct
8 Correct 158 ms 2528 KB Output is correct
9 Correct 161 ms 2528 KB Output is correct
10 Correct 154 ms 2528 KB Output is correct
11 Correct 157 ms 2464 KB Output is correct
12 Correct 157 ms 2528 KB Output is correct
13 Correct 170 ms 2656 KB Output is correct
14 Correct 164 ms 2528 KB Output is correct
15 Correct 173 ms 2576 KB Output is correct
16 Correct 170 ms 2528 KB Output is correct
17 Correct 159 ms 2528 KB Output is correct
18 Correct 1634 ms 16968 KB Output is correct
19 Correct 1586 ms 16840 KB Output is correct
20 Correct 1792 ms 17100 KB Output is correct
21 Correct 177 ms 2528 KB Output is correct
22 Correct 149 ms 2528 KB Output is correct
23 Correct 493 ms 8620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 165 ms 2544 KB Output is correct
3 Correct 171 ms 2528 KB Output is correct
4 Correct 1802 ms 16968 KB Output is correct
5 Correct 175 ms 2528 KB Output is correct
6 Correct 167 ms 2572 KB Output is correct
7 Correct 169 ms 2528 KB Output is correct
8 Correct 172 ms 2528 KB Output is correct
9 Correct 172 ms 2656 KB Output is correct
10 Correct 171 ms 2528 KB Output is correct
11 Correct 167 ms 2572 KB Output is correct
12 Correct 157 ms 2676 KB Output is correct
13 Correct 174 ms 2528 KB Output is correct
14 Correct 1752 ms 16840 KB Output is correct
15 Correct 172 ms 2556 KB Output is correct
16 Correct 1571 ms 16860 KB Output is correct
17 Correct 1612 ms 16988 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 16 ms 748 KB Output is correct
3 Correct 18 ms 748 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 0 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 16 ms 748 KB Output is correct
17 Correct 170 ms 2528 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 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 0 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 1114 ms 16840 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1600 ms 16876 KB Output is correct
31 Correct 1674 ms 17012 KB Output is correct
32 Correct 1623 ms 17060 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 1668 ms 17024 KB Output is correct
35 Correct 1646 ms 16880 KB Output is correct
36 Correct 1670 ms 16840 KB Output is correct
37 Correct 1771 ms 16940 KB Output is correct
38 Correct 0 ms 364 KB Output is correct
39 Correct 159 ms 2520 KB Output is correct
40 Correct 165 ms 2528 KB Output is correct
41 Correct 158 ms 2528 KB Output is correct
42 Correct 161 ms 2528 KB Output is correct
43 Correct 154 ms 2528 KB Output is correct
44 Correct 157 ms 2464 KB Output is correct
45 Correct 157 ms 2528 KB Output is correct
46 Correct 170 ms 2656 KB Output is correct
47 Correct 164 ms 2528 KB Output is correct
48 Correct 173 ms 2576 KB Output is correct
49 Correct 170 ms 2528 KB Output is correct
50 Correct 159 ms 2528 KB Output is correct
51 Correct 1634 ms 16968 KB Output is correct
52 Correct 1586 ms 16840 KB Output is correct
53 Correct 1792 ms 17100 KB Output is correct
54 Correct 177 ms 2528 KB Output is correct
55 Correct 149 ms 2528 KB Output is correct
56 Correct 493 ms 8620 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 165 ms 2544 KB Output is correct
59 Correct 171 ms 2528 KB Output is correct
60 Correct 1802 ms 16968 KB Output is correct
61 Correct 175 ms 2528 KB Output is correct
62 Correct 167 ms 2572 KB Output is correct
63 Correct 169 ms 2528 KB Output is correct
64 Correct 172 ms 2528 KB Output is correct
65 Correct 172 ms 2656 KB Output is correct
66 Correct 171 ms 2528 KB Output is correct
67 Correct 167 ms 2572 KB Output is correct
68 Correct 157 ms 2676 KB Output is correct
69 Correct 174 ms 2528 KB Output is correct
70 Correct 1752 ms 16840 KB Output is correct
71 Correct 172 ms 2556 KB Output is correct
72 Correct 1571 ms 16860 KB Output is correct
73 Correct 1612 ms 16988 KB Output is correct
74 Correct 1 ms 364 KB Output is correct
75 Correct 1 ms 364 KB Output is correct
76 Correct 1 ms 364 KB Output is correct
77 Correct 1 ms 364 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 16 ms 1132 KB Output is correct
80 Correct 1815 ms 26244 KB Output is correct
81 Correct 1799 ms 26132 KB Output is correct
82 Correct 1782 ms 26056 KB Output is correct
83 Correct 1783 ms 26320 KB Output is correct
84 Correct 1760 ms 26056 KB Output is correct
85 Correct 1785 ms 26280 KB Output is correct
86 Correct 534 ms 18644 KB Output is correct
87 Correct 1 ms 364 KB Output is correct