P13535 [IOI 2025] 纪念品(souvenirs)
Genius_Star · · 题解
思路:
大神题。
考虑递归,我们令
对于一个
然后考虑子问题用
显然最终会先递归到
最后你已经求出了
完整代码:
#include <utility>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
std::pair<std::vector<int>, long long> transaction(long long M);
const int N = 105;
ll val[N];
int cnt[N];
inline int dfs(ll w, int now){
auto t = transaction(w);
auto A = t.fi;
ll r = t.se;
w -= r;
for(auto v : A)
++cnt[v];
while(A.back() >= now){
w -= val[A.back()];
A.pop_back();
}
while(A.front() + 1 < now){
now = dfs((w - 1) / A.size(), now);
while(A.back() >= now){
w -= val[A.back()];
A.pop_back();
}
}
val[A.front()] = w;
return A.front();
}
void buy_souvenirs(int N, long long P0){
dfs(P0 - 1, N);
for(int i = 1; i < N; ++i){
while(cnt[i] < i){
++cnt[i];
transaction(val[i]);
}
}
}
注意:这是在某交互大神帮助下完成的该题,并非独立做出。