Problem p03560 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#ifdef TESTING
#include "dump.hpp"
#else
#define DUMP(...)
#define DEBUG(...)
#endif

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s)                                                      \
  for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s));           \
       (0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e)                                                         \
  for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1);            \
       (_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n)                                                                 \
  int n;                                                                       \
  assert(0 < scanf("%d", &n))

int main() {
  INT(k);
  vector<vector<int>> vvi(55);
  vector<int> v(k, -1);
  v[1] = 1;
  vvi[1].PB(1);
  REP(i, 1, 55) {
    if (0 < v[0]) {
      break;
    }
    int j = 0;
    while (j < SZ(vvi[i])) {
      int n = (vvi[i][j] * 10) % k;
      if (v[n] < 0) {
        v[n] = i;
        vvi[i].PB(n);
      }
      j++;
    }
    for (int n1 : vvi[1]) {
      for (int ni : vvi[i]) {
        int n = (n1 + ni) % k;
        if (v[n] < 0) {
          v[n] = i + 1;
          vvi[i + 1].PB(n);
        }
      }
    }
  }
  printf("%d\n", v[0]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#ifdef TESTING
#include "dump.hpp"
#else
#define DUMP(...)
#define DEBUG(...)
#endif

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s)                                                      \
  for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s));           \
       (0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e)                                                         \
  for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1);            \
       (_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n)                                                                 \
  int n;                                                                       \
  assert(0 < scanf("%d", &n))

int main() {
  INT(k);
  vector<vector<int>> vvi(55);
  vector<int> v(k, -1);
  v[1] = 1;
  vvi[1].PB(1);
  REP(i, 1, 55) {
    if (0 < v[0]) {
      break;
    }
    int j = 0;
    while (j < SZ(vvi[i])) {
      int n = (vvi[i][j] * 10) % k;
      if (v[n] < 0) {
        v[n] = i;
        vvi[i].PB(n);
      }
      j++;
    }
    for (int ni : vvi[i]) {
      int n = (ni + 1) % k;
      if (v[n] < 0) {
        v[n] = i + 1;
        vvi[i + 1].PB(n);
      }
    }
  }
  printf("%d\n", v[0]);
  return 0;
}