CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Numerot
Sender:PallomerenPiikki
Submission time:2020-10-18 12:50:57 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:88:17: error: no matching function for call to 'min(long long int&, int)'
   t = min(t, 100);
                 ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from input/code.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:195:5: note: candidate: template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)
     min(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:195:5: note:   template argument deduction/substitution failed:
input/code.cpp:88:17: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   t = min(t, 100);
                 ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-lin...

Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>

const int MAXDC = 20;
const int BASE = 10;

int pot[MAXDC+2];
pii dp[MAXDC][BASE][BASE][BASE];

struct Num {
  int val;
  Num(int x=0) : val(x) {}
  int operator[](int pos) const {
    return val / pot[pos] % 10;
  }
  int tzc() const {
    for (int i=2;; i++)
      if (val % pot[i] >= 10) return i-1;
  }
};

pii set_zeros(int k, int count) {
  int totsteps = 0;
  int totdec = 0;
  for (;;) {
    int rem = k % pot[count];
    if (rem < 10 && totsteps) break;
    if (k < 10) break;
    int zc = Num(k).tzc();
    int maxd = 0;
    for (int i=zc+1; i<MAXDC; i++)
      maxd = max(maxd, Num(k)[i]);
    int first = Num(k)[zc];
    int last = Num(k)[0];
    auto [steps, dec] = dp[zc][maxd][first][last];
    if (steps == 0) {
      steps = 1;
      dec = max(maxd, max(first, last));
    }
    totsteps += steps;
    totdec += dec;
    k -= dec;
  }
  return pii(totsteps, totdec);
}

int f(int k) {
  auto [steps, dec] = set_zeros(k, MAXDC);
  return steps + (k-dec>0);
}

int solve(int x) {
  int k = 0;
  int maxk = 1 + 9*x;
  if (x >= 1e6) {
    k = 8 * x;
  }
  for (int b=maxk; b >= 1; b /= 2) {
    while (k+b < maxk && f(k+b) < x) k += b;
  }
  while (f(k) < x) k++;
  return k;
}

void init() {
  pot[0] = 1;
  for (int i=1; i<MAXDC+2; i++)
    pot[i] = 10 * pot[i-1];
  for (int zc=1; zc<MAXDC; zc++) {
    for (int maxd=0; maxd<=9; maxd++) {
      for (int first=0; first<=9; first++) {
        for (int mod10=0; mod10<=9; mod10++) {
          int k = pot[zc+1]*maxd + pot[zc]*first + mod10;
          dp[zc][maxd][first][mod10] = set_zeros(k, zc);
        }
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  init();
  int t;
  cin >> t;
  t = min(t, 100);
  while (t--) {
    int x;
    cin >> x;
    int k = solve(x);
    if (f(k) == x) {
      cout << k << '\n';
      cerr << fixed << 1.0*k/x << '\n';
    }
    else {
      cout << "-1\n";
    }
  }
}