Submission #8244091


Source Code Expand

Copy
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
/**
* @note O(\sqrt{n})
* @note about 1.0 sec for 10^5 queries with n < 10^10
*/
struct prepared_primes {
int size;
vector<int> sieve;
vector<int> primes;
prepared_primes(int size_)
: size(size_) {
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }

/**
 * @note O(\sqrt{n})
 * @note about 1.0 sec for 10^5 queries with n < 10^10
 */
struct prepared_primes {
    int size;
    vector<int> sieve;
    vector<int> primes;

    prepared_primes(int size_)
        : size(size_) {

        sieve.resize(size);
        REP3 (p, 2, size) if (sieve[p] == 0) {
            primes.push_back(p);
            for (int k = p; k < size; k += p) {
                if (sieve[k] == 0) {
                    sieve[k] = p;
                }
            }
        }
    }

    vector<int64_t> prime_factorize(int64_t n) {
        assert (1 <= n and n < (int64_t)size * size);
        vector<int64_t> result;

        // trial division for large part
        for (int p : primes) {
            if (n < size or n < (int64_t)p * p) {
                break;
            }
            while (n % p == 0) {
                n /= p;
                result.push_back(p);
            }
        }

        // small part
        if (n == 1) {
            // nop
        } else if (n < size) {
            while (n != 1) {
                result.push_back(sieve[n]);
                n /= sieve[n];
            }
        } else {
            result.push_back(n);
        }

        assert (is_sorted(ALL(result)));
        return result;
    }

    vector<int64_t> list_all_factors(int64_t n) {
        auto p = prime_factorize(n);
        vector<int64_t> d;
        d.push_back(1);
        for (int l = 0; l < p.size(); ) {
            int r = l + 1;
            while (r < p.size() and p[r] == p[l]) ++ r;
            int n = d.size();
            REP (k1, r - l) {
                REP (k2, n) {
                    d.push_back(d[d.size() - n] * p[l]);
                }
            }
            l = r;
        }
        return d;
    }
};


int64_t solve(int n, const vector<int64_t> & s) {
    int sqrt_n = sqrt(n) + 3;

    vector<vector<vector<int64_t> > > sum(sqrt_n);
    REP3 (delta, 1, sqrt_n) {
        sum[delta].resize(delta, vector<int64_t>(1));
        REP (i, n) {
            int offset = i % delta;
            sum[delta][offset].push_back(sum[delta][offset].back() + s[i]);
        }
    }
    auto calc_small = [&](int a, int b) -> int64_t {
        assert (a - b < sqrt_n);
        int k = (n - 1 - a) / (a - b);
        if (a % (a - b) == 0 and a / (a - b) <= k) return 0;
        int64_t fst = sum[a - b][a % (a - b)].back() - sum[a - b][a % (a - b)][a / (a - b)];
        int64_t snd = sum[a - b][0][k + 1];
        return fst + snd;
    };

    vector<int> used(n);
    int current = 0;
    auto calc_large = [&](int a, int b) -> int64_t {
        int x = 0;
        int64_t acc = 0;
        ++ current;
        for (int i = 0; ; ++ i) {
            used[x] = current;
            x = (i % 2 == 0 ? x + a : x - b);
            if (x == n - 1) return acc;
            if (x < 0 or n <= x or used[x] == current) return 0;
            acc += s[x];
        }
    };

    int64_t answer = 0;
    prepared_primes primes(sqrt_n);
    REP3 (a, 1, n - 1) {
        for (int k : primes.list_all_factors(n - 1 - a)) {
            int b = a - (n - 1 - a) / k;
            if (b <= 0 or a - b <= 0) continue;
            assert (k * (a - b) + a == n - 1);  // there are almost 10^6 pairs of (A, B) when N = 10^5
            chmax(answer, a - b < sqrt_n ? calc_small(a, b) : calc_large(a, b));
        }
    }
    return answer;
}

int main() {
    int n; cin >> n;
    vector<int64_t> s(n);
    REP (i, n) {
        cin >> s[i];
    }
    cout << solve(n, s) << endl;
    return 0;
}

Submission Info

Submission Time
Task F - Frog Jump
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3952 Byte
Status AC
Exec Time 682 ms
Memory 349196 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 75
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 1 ms 256 KB
02.txt AC 1 ms 256 KB
03.txt AC 1 ms 256 KB
04.txt AC 1 ms 256 KB
05.txt AC 1 ms 256 KB
06.txt AC 1 ms 256 KB
07.txt AC 1 ms 256 KB
08.txt AC 1 ms 256 KB
09.txt AC 1 ms 256 KB
10.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
13.txt AC 1 ms 256 KB
14.txt AC 1 ms 256 KB
15.txt AC 52 ms 26112 KB
16.txt AC 310 ms 163452 KB
17.txt AC 581 ms 295288 KB
18.txt AC 164 ms 78588 KB
19.txt AC 649 ms 341496 KB
20.txt AC 593 ms 313720 KB
21.txt AC 634 ms 320888 KB
22.txt AC 633 ms 322936 KB
23.txt AC 655 ms 343800 KB
24.txt AC 653 ms 343800 KB
25.txt AC 674 ms 343800 KB
26.txt AC 675 ms 343800 KB
27.txt AC 672 ms 343800 KB
28.txt AC 315 ms 169052 KB
29.txt AC 328 ms 168060 KB
30.txt AC 361 ms 194936 KB
31.txt AC 377 ms 194936 KB
32.txt AC 597 ms 317304 KB
33.txt AC 637 ms 333944 KB
34.txt AC 578 ms 298104 KB
35.txt AC 609 ms 309240 KB
36.txt AC 625 ms 318072 KB
37.txt AC 670 ms 336376 KB
38.txt AC 632 ms 325684 KB
39.txt AC 621 ms 317560 KB
40.txt AC 601 ms 308344 KB
41.txt AC 626 ms 319736 KB
42.txt AC 641 ms 326392 KB
43.txt AC 633 ms 323064 KB
44.txt AC 671 ms 342264 KB
45.txt AC 582 ms 298872 KB
46.txt AC 660 ms 336248 KB
47.txt AC 661 ms 349196 KB
48.txt AC 650 ms 343800 KB
49.txt AC 677 ms 343800 KB
50.txt AC 674 ms 343800 KB
51.txt AC 675 ms 343800 KB
52.txt AC 676 ms 343800 KB
53.txt AC 671 ms 343800 KB
54.txt AC 678 ms 343800 KB
55.txt AC 682 ms 343800 KB
56.txt AC 674 ms 347900 KB
57.txt AC 675 ms 343800 KB
58.txt AC 676 ms 343800 KB
59.txt AC 675 ms 343800 KB
60.txt AC 672 ms 343800 KB
61.txt AC 674 ms 343800 KB
62.txt AC 674 ms 343800 KB
63.txt AC 676 ms 343800 KB
64.txt AC 673 ms 343800 KB
65.txt AC 674 ms 347404 KB
66.txt AC 671 ms 343800 KB
67.txt AC 458 ms 247288 KB
68.txt AC 476 ms 247288 KB
69.txt AC 476 ms 247288 KB
70.txt AC 476 ms 247288 KB
71.txt AC 475 ms 247288 KB
72.txt AC 478 ms 247288 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB
s3.txt AC 1 ms 256 KB


2025-04-05 (Sat)
18:22:30 +00:00