QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110826 | #3869. Gastronomic Event | gigabuffoon | WA | 371ms | 53700kb | C++20 | 4.7kb | 2023-06-04 02:32:44 | 2023-06-04 02:32:45 |
Judging History
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]) {
int full = sz(dp) - 1 + num[k]*k;
vector<vector<bool>> next(k, vector<bool>((full + k - 1) / k));
dp.resize(full);
rep (i, 0, k)
for (int p = 0, j = i; j < sz(dp); p++, j += k)
next[i][p] = dp[j];
rep (i, 0, k) {
int on = 0;
rep (j, 0, sz(next[i])) {
on += next[i][j];
if (i + j*k < sz(dp))
dp[i + j*k] = dp[i + j*k] || !!on;
if (j >= num[k])
on -= next[i][j - 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: 1ms
memory: 3396kb
input:
5 1 2 2 2
output:
13
result:
ok single line: '13'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3448kb
input:
10 1 2 3 4 3 2 7 8 7
output:
47
result:
ok single line: '47'
Test #3:
score: 0
Accepted
time: 172ms
memory: 51632kb
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: 371ms
memory: 30992kb
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: -100
Wrong Answer
time: 132ms
memory: 53700kb
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:
249978349548
result:
wrong answer 1st lines differ - expected: '249978362970', found: '249978349548'