QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#110830#3869. Gastronomic EventgigabuffoonTL 1853ms30648kbC++204.5kb2023-06-04 02:43:372023-06-04 02:43:38

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-04 02:43:38]
  • 评测
  • 测评结果:TL
  • 用时:1853ms
  • 内存:30648kb
  • [2023-06-04 02:43:37]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(a, b, c) for (int a = (b); a < (c); a++)
#define sz(x) int(size(x))
#define all(x) begin(x), end(x)
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

const ll MOD = (119 << 23) + 1;
ll modpow(ll a, ll n) {
    ll ans = 1;
    for (; n > 0; n /= 2, a = a * a % MOD)
        if (n & 1) ans = ans * a % MOD;
    return ans;
}


const ll mod = (119 << 23) + 1, root = 62;
typedef vector<ll> vl;
void ntt(vl& a) {
    int n = sz(a), L = 31 - __builtin_clz(n);
    static vl rt(2, 1);
    for (static int k = 2, s = 2; k < n; k *= 2, s++) {
        rt.resize(n);
        ll z[] = {1, modpow(root, mod >> s)};
        rep (i, k, 2*k) rt[i] = rt[i / 2] * z[i & 1] % mod;
    }
    vi rev(n);
    rep (i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
    rep (i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (int k = 1; k < n; k *= 2)
        for (int i = 0; i < n; i += 2*k) rep (j, 0, k) {
            ll z = rt[j + k] * a[i + j + k] % mod, &ai = a[i + j];
            a[i + j + k] = ai - z + (z > ai ? mod : 0);
            ai += (ai + z >= mod ? z - mod : z);
        }
}

// const int LIM = 1024;
// template<int K = 32>
// vl slowConv(const vl& a, const vl& b) {
//     if (K < sz(a) + sz(b) - 1) return slowConv<min(2*K, LIM)>(a, b);
//     using bs = bitset<K>;
    
//     bs B, C;
//     rep (i, 0, sz(b)) B[i] = b[i];
    
//     rep (i, 0, sz(a)) if (a[i]) C |= B << i;

//     vl c(sz(a) + sz(b) - 1);
//     rep (i, 0, sz(c)) c[i] = C[i];
//     return c;
// }
const int LIM = 2048;
// template<int K = 32>
vl slowConv(const vl& a, const vl& b) {
    // if (K < sz(a) + sz(b) - 1) return slowConv<min(2*K, LIM)>(a, b);
    using bs = bitset<LIM>;
    
    bs B, C;
    rep (i, 0, sz(b)) B[i] = b[i];
    
    rep (i, 0, sz(a)) if (a[i]) C |= B << i;

    vl c(sz(a) + sz(b) - 1);
    rep (i, 0, sz(c)) c[i] = C[i];
    return c;
}

vl conv(const vl& a, const vl& b) {
    if (a.empty() || b.empty()) return {};
    int s = sz(a) + sz(b) - 1, B = 32 - __builtin_clz(s), n = 1 << B;

    if (s <= LIM) return slowConv(a, b);

    int inv = modpow(n, mod - 2);
    vl L(a), R(b), out(n);
    L.resize(n), R.resize(n);
    ntt(L), ntt(R);
    rep (i, 0, n) out[-i & (n-1)] = (ll)L[i] * R[i] % mod * inv % mod;
    ntt(out);
    rep (i, 0, s) out[i] = !!out[i];
    return {out.begin(), out.begin() + s};
}

void solve() {
    int n;
    cin >> n;

    vi p(n, -1);
    rep (i, 1, n) cin >> p[i], p[i]--;

    vi sub(n, 1), big(n);

    for (int i = n-1; i > 0; i--) {
        if (i > 0) {
            sub[p[i]] += sub[i];
            big[p[i]] = max(big[p[i]], sub[i]);
        }
        big[i] = max(big[i], n - sub[i]);
    }

    int rt = min_element(all(big)) - begin(big);

    vector<vl> f;
    priority_queue<pii> q;

    vi num(n+1);

    int cap = min(n / 2, n - 1 - big[rt]) + 1;
    ll ans = big[rt] * ll(n - 1 - big[rt]);

    rep (i, 0, n) if (p[i] == rt) {
        int k = sub[i];
        num[k]++;
    }

    if (n - sub[rt] > 0) {
        int k = n - sub[rt];
        num[k]++;
    }

    {
        vector<bool> dp(1);
        dp[0] = 1;

        for (int k = 1; k <= n; k++) if (num[k]) {
            dp.resize(sz(dp) + num[k]*k);

            vector<bool> next((sz(dp) + k - 1) / k + 1);
            rep (i, 0, k) {
                for (int p = 0, j = i; j < sz(dp); p++, j += k)
                    next[p] = dp[j];

                int on = 0;
                for (int p = 0, j = i; j < sz(dp); p++, j += k) {
                    on += next[p];

                    dp[j] = dp[j] || on;

                    if (p >= num[k]) on -= next[p - num[k]];
                }
            }
        }

        rep (i, 0, sz(dp)) if (dp[i]) ans = max(ans, i * ll(n - 1 - i));
    }

    vi down(n, -1);
    for (int x = rt, y = p[x]; x != 0; x = y, y = p[x]) {
        down[y] = x;
        p[x] = -1;
    }
    
    vector<ll> dp(n, 1);

    for (int i = n-1; i >= 0; i--) {
        if (p[i] != -1) 
            dp[p[i]] += dp[i];

        if (down[i] == -1 && i != rt) ans += dp[i];
    }

    for (int i = 0; i < n; i++) {
        if (down[i] != -1) {
            dp[down[i]] += dp[i];
            ans += dp[i];
        }
    }

    ans += dp[rt];
    cout << ans << "\n";
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    // int t; cin >> t; while (t--)
    solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3384kb

input:

5
1 2 2 2

output:

13

result:

ok single line: '13'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3452kb

input:

10
1 2 3 4 3 2 7 8 7

output:

47

result:

ok single line: '47'

Test #3:

score: 0
Accepted
time: 93ms
memory: 30500kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 35 38 39 40 40 39 43 38 45 35 35 34 34 34 33 52 53 54 55 56 56 58 56 60 60 60 63 56 65 65 55 54 53 70 71 72 71 74 70 76 77 78 79 76 81 82 70 84 70 70 53 88 89 90 91 90 89 94 95 96 94 89 88 100 ...

output:

249834400049

result:

ok single line: '249834400049'

Test #4:

score: 0
Accepted
time: 260ms
memory: 30644kb

input:

1000000
1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...

output:

250003506532

result:

ok single line: '250003506532'

Test #5:

score: 0
Accepted
time: 93ms
memory: 30548kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 27 31 31 33 34 35 36 37 38 39 40 41 42 43 43 45 46 47 48 49 50 51 52 53 54 53 52 57 58 59 59 57 62 63 64 65 66 67 65 63 70 70 63 73 73 62 76 76 78 79 62 81 82 83 62 85 86 85 88 85 52 91 51 93 94 51 96 97 98 99 100 ...

output:

249978362970

result:

ok single line: '249978362970'

Test #6:

score: 0
Accepted
time: 424ms
memory: 30536kb

input:

1000000
1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ...

output:

250002449323

result:

ok single line: '250002449323'

Test #7:

score: 0
Accepted
time: 104ms
memory: 30500kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

250193522166

result:

ok single line: '250193522166'

Test #8:

score: 0
Accepted
time: 78ms
memory: 30488kb

input:

999997
1 2 3 3 2 6 6 2 9 9 2 12 13 2 15 15 2 18 19 2 21 22 2 24 25 2 27 28 2 30 31 2 33 33 2 36 37 2 39 40 2 42 43 2 45 45 2 48 48 2 51 52 2 54 55 2 57 57 2 60 61 2 63 63 2 66 66 2 69 69 2 72 72 2 75 76 2 78 78 2 81 81 2 84 84 2 87 87 2 90 91 2 93 94 2 96 97 2 99 99 2 102 103 2 105 106 2 108 108 2 1...

output:

250000666076

result:

ok single line: '250000666076'

Test #9:

score: 0
Accepted
time: 49ms
memory: 14580kb

input:

420001
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 26 30 31 32 31 30 35 26 26 38 25 40 41 42 43 43 43 42 41 48 49 48 25 52 53 54 54 53 57 58 58 52 25 62 63 25 65 66 67 66 65 65 71 24 73 74 75 76 77 77 76 75 81 75 83 84 85 83 74 88 88 90 74 92 74 74 73 96 97 96 73 100 1...

output:

44105186491

result:

ok single line: '44105186491'

Test #10:

score: 0
Accepted
time: 34ms
memory: 14628kb

input:

420121
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 31 29 34 27 36 37 36 39 40 27 42 27 26 45 45 47 48 49 48 47 52 53 45 55 56 57 58 59 57 61 62 57 57 56 56 67 68 67 56 26 72 25 74 75 76 77 77 77 76 81 76 75 84 85 75 75 74 89 25 91 92 92 94 94 91 25 98 99 98 10...

output:

44130371130

result:

ok single line: '44130371130'

Test #11:

score: 0
Accepted
time: 87ms
memory: 30492kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 20 22 22 20 25 26 27 28 29 30 28 32 32 28 28 36 27 38 39 39 38 38 38 26 26 46 47 47 26 25 51 52 53 53 52 56 57 58 56 52 61 62 62 64 65 66 67 66 65 62 71 71 71 71 62 76 77 78 78 77 81 76 83 83 76 86 52 51 51 51 51 92 92 20 95 96 97 96 99 96 1...

output:

250007600773

result:

ok single line: '250007600773'

Test #12:

score: 0
Accepted
time: 453ms
memory: 30600kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 20 22 23 24 25 26 27 26 29 29 25 24 33 34 35 36 37 34 39 40 34 42 33 44 24 46 24 48 24 22 51 52 53 54 55 56 56 53 52 60 61 61 63 60 65 66 65 68 52 51 51 22 73 73 75 73 73 22 79 80 81 79 79 84 20 86 87 88 89 90 89 19 93 94 95 95 97 94 99 93 1...

output:

250007340814

result:

ok single line: '250007340814'

Test #13:

score: 0
Accepted
time: 82ms
memory: 30492kb

input:

999999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 25 28 28 30 30 28 33 25 24 36 37 37 39 37 41 42 37 36 45 46 47 46 45 50 45 52 24 54 23 56 57 58 59 60 61 62 62 62 61 60 59 68 68 59 71 71 58 74 74 76 58 57 79 80 81 81 83 80 85 80 87 88 89 89 80 80 79 79 57 56 56 98 99 100 9...

output:

250012392132

result:

ok single line: '250012392132'

Test #14:

score: 0
Accepted
time: 135ms
memory: 30512kb

input:

999999
1 2 3 4 5 6 7 8 9 10 11 12 12 11 11 11 17 11 19 11 21 11 11 11 11 11 11 11 11 10 31 32 32 32 32 32 32 31 39 31 41 31 43 31 45 31 31 31 31 31 31 31 31 31 31 31 31 10 59 59 59 59 63 59 59 59 59 59 59 59 10 72 73 73 72 72 72 72 72 72 72 72 72 10 85 85 85 85 85 85 10 92 93 94 93 93 93 93 93 93 92...

output:

250004726953

result:

ok single line: '250004726953'

Test #15:

score: 0
Accepted
time: 95ms
memory: 30564kb

input:

999999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...

output:

245199646373

result:

ok single line: '245199646373'

Test #16:

score: 0
Accepted
time: 284ms
memory: 30520kb

input:

1000000
1 2 3 4 5 6 7 8 7 7 11 7 13 7 15 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7...

output:

250003475896

result:

ok single line: '250003475896'

Test #17:

score: 0
Accepted
time: 1248ms
memory: 30560kb

input:

1000000
1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ...

output:

250002634697

result:

ok single line: '250002634697'

Test #18:

score: 0
Accepted
time: 443ms
memory: 30604kb

input:

1000000
1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ...

output:

250002449642

result:

ok single line: '250002449642'

Test #19:

score: 0
Accepted
time: 216ms
memory: 30500kb

input:

1000000
1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...

output:

250002222111

result:

ok single line: '250002222111'

Test #20:

score: 0
Accepted
time: 120ms
memory: 30556kb

input:

1000000
1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...

output:

250001648514

result:

ok single line: '250001648514'

Test #21:

score: 0
Accepted
time: 96ms
memory: 30556kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

250119221665

result:

ok single line: '250119221665'

Test #22:

score: 0
Accepted
time: 1833ms
memory: 30644kb

input:

1000000
1 2 3 3 3 6 3 8 3 10 10 3 13 13 3 16 16 16 3 20 20 20 3 24 24 24 24 3 29 29 29 29 3 34 34 34 34 34 3 40 40 40 40 40 3 46 46 46 46 46 46 3 53 53 53 53 53 53 3 60 60 60 60 60 60 60 3 68 68 68 68 68 68 68 3 76 76 76 76 76 76 76 76 3 85 85 85 85 85 85 85 85 3 94 94 94 94 94 94 94 94 94 3 104 104...

output:

250002575077

result:

ok single line: '250002575077'

Test #23:

score: 0
Accepted
time: 2ms
memory: 3436kb

input:

2
1

output:

3

result:

ok single line: '3'

Test #24:

score: 0
Accepted
time: 1835ms
memory: 30580kb

input:

999999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...

output:

250318617239

result:

ok single line: '250318617239'

Test #25:

score: 0
Accepted
time: 1829ms
memory: 30648kb

input:

1000000
1 2 3 3 3 6 3 8 3 10 10 3 13 13 3 16 16 16 3 20 20 20 3 24 24 24 24 3 29 29 29 29 3 34 34 34 34 34 3 40 40 40 40 40 3 46 46 46 46 46 46 3 53 53 53 53 53 53 3 60 60 60 60 60 60 60 3 68 68 68 68 68 68 68 3 76 76 76 76 76 76 76 76 3 85 85 85 85 85 85 85 85 3 94 94 94 94 94 94 94 94 94 3 104 104...

output:

250002497999

result:

ok single line: '250002497999'

Test #26:

score: 0
Accepted
time: 1838ms
memory: 30576kb

input:

999999
1 2 3 4 5 6 7 8 9 10 10 10 13 10 15 10 17 17 10 20 20 10 23 24 23 10 27 27 29 10 31 32 33 33 10 36 37 36 36 10 41 42 43 41 45 10 47 48 47 50 47 10 53 54 54 56 56 53 10 60 61 62 62 61 65 10 67 68 68 67 71 67 73 10 75 76 77 78 78 80 76 10 83 84 85 84 87 84 89 83 10 92 93 92 95 95 97 98 92 10 10...

output:

250006990483

result:

ok single line: '250006990483'

Test #27:

score: 0
Accepted
time: 1853ms
memory: 30528kb

input:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

250334332500

result:

ok single line: '250334332500'

Test #28:

score: -100
Time Limit Exceeded

input:

999999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...

output:


result: