#include <bits/stdc++.h>
#include "souvenirs.h"
using namespace std;
#define ll long long
void buy_souvenirs(int n, long long P0)
{
vector<ll> p(n), used(n);
p[0] = P0;
vector<pair<vector<int>, ll>> mp;
map<ll, pair<vector<int>, ll>> ehh;
auto ch = [&](ll x) -> pair<vector<int>, ll>
{
if (ehh.count(x))
return ehh[x];
pair<vector<int>, ll> hm = transaction(x);
for (auto &i : hm.first)
used[i]++;
hm.second = x - hm.second;
mp.push_back(hm);
return ehh[x] = hm;
};
auto rec = [&](auto rec, int i) -> void
{
if (i == n - 1)
return;
pair<vector<int>, ll> cur;
ll s = p[i] - 1;
int sz = 2;
while (1)
{
cur = ch(s);
s -= cur.second;
sz = cur.first.size();
if (sz == 1)
break;
s = s / sz;
}
p[cur.first[0]] = s, rec(rec, cur.first[0]);
for (auto &x : mp)
{
int s = x.second, c = 0, j = 0;
for (auto &i : x.first)
{
if (p[i] == 0)
c++, j = i;
s -= p[i];
}
if (c == 1)
p[j] = s;
}
int last = i;
for (int j = i; j < cur.first[0]; j++)
{
if (p[j] == 0)
ch(p[last] - 1);
else
last = i;
}
for (auto &x : mp)
{
int s = x.second, c = 0, j = 0;
for (auto &i : x.first)
{
if (p[i] == 0)
c++, j = i;
s -= p[i];
}
if (c == 1)
p[j] = s;
}
};
rec(rec, 0);
for (int i = 0; i < n; i++)
for (int j = used[i]; j < i; j++)
transaction(p[i]);
return;
}
# | 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... |