Problem p03560 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10);
  int k;
  cin >> k;
  while (k % 2 == 0)
    k /= 2;
  while (k % 5 == 0)
    k /= 5;
  int loop = 0;
  int m = 1;
  vector<int> edge;
  vector<int> f(k, 0);
  do {
    // edge.push_back(m);
    if (f[m] == 0)
      f[m] = 1;
    else
      break;
    m *= 10;
    m %= k;
    loop++;
  } while (m != 1);
  int l = 0;
  rep(i, k) l += f[m];
  edge.resize(l);
  l = 0;
  rep(i, k) {
    if (f[i]) {
      edge[l] = i;
      l++;
    }
  }
  // cout<<loop<<endl;
  //   cout<<k<<endl;
  if (k == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (loop == k - 1) {
    cout << 2 << endl;
    return 0;
  }
  vector<int> dist(k, 0);

  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int i : edge) {
      int tmp = (i + now);
      if (tmp >= k)
        tmp -= k;
      if (dist[tmp] > 0)
        continue;
      dist[tmp] = dist[now] + 1;
      if (tmp == 0) {
        cout << dist[tmp] + 1 << endl;
        return 0;
      }
      que.push(tmp);
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10);
  int k;
  cin >> k;
  while (k % 2 == 0)
    k /= 2;
  while (k % 5 == 0)
    k /= 5;
  int loop = 0;
  int m = 1;
  vector<int> edge;
  vector<int> f(k, 0);
  do {
    // edge.push_back(m);
    if (f[m] == 0)
      f[m] = 1;
    else
      break;
    m *= 10;
    m %= k;
    loop++;
  } while (m != 1);
  int l = 0;
  rep(i, k) l += f[i];
  // cout<<l<<endl;
  edge.resize(l);
  l = 0;
  rep(i, k) {
    if (f[i]) {
      edge[l] = i;
      l++;
    }
  }
  // cout<<loop<<endl;
  //   cout<<k<<endl;
  if (k == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (loop == k - 1) {
    cout << 2 << endl;
    return 0;
  }
  vector<int> dist(k, 0);

  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int i : edge) {
      int tmp = (i + now);
      if (tmp >= k)
        tmp -= k;
      if (dist[tmp] > 0)
        continue;
      dist[tmp] = dist[now] + 1;
      if (tmp == 0) {
        cout << dist[tmp] + 1 << endl;
        return 0;
      }
      que.push(tmp);
    }
  }

  return 0;
}