49912023-04-08 15:46:44TomaSajtHázszámokcpp17Accepted 100/1004ms5560 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int base;

map<pair<ll, int>, ll> mem;
// I have no idea how this works, just started messing around with formulas on
// OEIS and got this working

// calculates the number of `d` digits used from `1..n`
ll pog(ll n, int d) {
  if (n <= 0)
    return 0;
  if (mem.count({n, d})) {
    return mem.at(pair<ll, int>(n, d));
  }
  ll r = n % base;
  ll q = n / base;
  ll res = q + (r + 1) * pog(q, d) + (base - r - 1) * pog(q - 1, d);
  if (d != 0 && r >= d) {
    res++;
  }
  return mem[pair<ll, int>(n, d)] = res;
}

int main() {
  cin >> base;
  vector<ll> budget(base);
  for (auto &b : budget)
    cin >> b;
  auto is_enough = [&](ll n) {
    for (int i = 0; i < base; i++) {
      ll a = pog(n, i);
      if (a > budget[i])
        return false;
    }
    return true;
  };

  ll curr = 0;
  for (int i = 55; i >= 0; i--) {
    ll next = curr + (1ll << i);
    if (is_enough(next)) {
      curr = next;
    }
  }
  cout << curr;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2208 KiB
3Accepted4ms2572 KiB
subtask225/25
4Accepted3ms2460 KiB
5Accepted3ms2672 KiB
6Accepted3ms2880 KiB
7Accepted3ms3272 KiB
8Accepted3ms3224 KiB
9Accepted3ms3404 KiB
10Accepted3ms3316 KiB
11Accepted4ms3512 KiB
12Accepted3ms3384 KiB
13Accepted3ms3716 KiB
14Accepted3ms3708 KiB
15Accepted3ms3684 KiB
16Accepted3ms3604 KiB
17Accepted3ms3988 KiB
18Accepted3ms3932 KiB
19Accepted3ms4264 KiB
subtask310/10
20Accepted4ms4320 KiB
21Accepted4ms4640 KiB
22Accepted4ms4812 KiB
23Accepted3ms4468 KiB
24Accepted4ms4772 KiB
subtask415/15
25Accepted4ms4780 KiB
26Accepted4ms4716 KiB
27Accepted4ms4664 KiB
28Accepted4ms4788 KiB
29Accepted4ms4788 KiB
30Accepted3ms4528 KiB
31Accepted4ms4888 KiB
32Accepted4ms4912 KiB
33Accepted4ms4812 KiB
subtask520/20
34Accepted3ms4608 KiB
35Accepted3ms4872 KiB
36Accepted4ms5032 KiB
37Accepted4ms5084 KiB
38Accepted4ms5092 KiB
39Accepted4ms5208 KiB
40Accepted4ms5040 KiB
41Accepted4ms5020 KiB
42Accepted4ms5116 KiB
subtask630/30
43Accepted4ms5376 KiB
44Accepted4ms5560 KiB
45Accepted4ms5296 KiB
46Accepted4ms5348 KiB
47Accepted4ms5348 KiB
48Accepted4ms5252 KiB
49Accepted3ms5072 KiB
50Accepted4ms5232 KiB
51Accepted4ms5240 KiB
52Accepted2ms4940 KiB
53Accepted4ms5244 KiB
54Accepted4ms5208 KiB
55Accepted4ms5140 KiB
56Accepted4ms5244 KiB
57Accepted4ms5288 KiB
58Accepted4ms5552 KiB
59Accepted4ms5240 KiB
60Accepted4ms5344 KiB