#include "souvenirs.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ari2 = array<int, 2>;
using ari3 = array<int, 3>;
using arl2 = array<ll, 2>;
using arl3 = array<ll, 3>;
template <class T> using vt = vector<T>;
#define all(x) begin(x), end(x)
#define size(x) (int((x).size()))
#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)
void buy_souvenirs(const int N, const ll P0) {
vt<ll> P(N);
P[0] = P0;
vt<int> cnt(N);
const auto dnc = [&](auto &&self, const ll v, int r) -> int {
auto [vec, rem] = transaction(v);
for (const auto &j : vec)
cnt[j]++;
while (true) {
ll used = v - rem;
int n = size(vec);
for (const auto &j : vec)
used -= P[j], n -= !!P[j];
if (n == 1)
P[vec[0]] = used;
if (r == vec[0])
break;
r = self(self, (used + n - 1) / n - 1, r) - 1;
}
return vec[0];
};
dnc(dnc, P[0]-1, N-1);
FOR(i, 1, N-1)
for (; cnt[i] < i; cnt[i]++)
transaction(P[i]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |