Problem p03560 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define CL fclose(stdin), fclose(stdout)
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
} // namespace io
int f[111111];
int dis[111111];
int q[111111], hq, tq;
int vis[111111];
int main() {
  int k = io::F();
  while (k % 2 == 0)
    k >>= 1;
  while (k % 5 == 0)
    k /= 5;
  if (k == 1)
    return puts("1"), 0;
  f[0] = 1;
  int pf = 0;
  for (register int i = 1; i < k; ++i) {
    f[i] = f[i - 1] * 10 % k;
    if (f[i] == 1) {
      pf = i;
      break;
    }
  }
  for (hq = 1, tq = 2, vis[0] = 1; hq != tq; ++hq) {
    for (register int i = 0; i < pf; ++i) {
      int to = (q[hq] + f[i]) % k;
      if (to == 0)
        return printf("%d\n", dis[q[hq]] + 1), 0;
      if (!vis[to])
        q[tq++] = to, dis[to] = dis[q[hq]] + 1, vis[to] = 1;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
#define CL fclose(stdin), fclose(stdout)
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = (n << 1) + (n << 3) + ch - '0';
  return F ? n : -n;
}
} // namespace io
int f[111111];
int dis[111111];
int q[111111], hq, tq;
int vis[111111];
int main() {
  int k = io::F();
  while (k % 2 == 0)
    k >>= 1;
  while (k % 5 == 0)
    k /= 5;
  if (k == 1)
    return puts("1"), 0;
  f[0] = 1;
  int pf = 0;
  for (register int i = 1; i < k; ++i) {
    f[i] = f[i - 1] * 10 % k;
    if (f[i] == 1) {
      pf = i;
      break;
    }
  }
  for (hq = 1, tq = 2, vis[0] = 1; hq != tq; ++hq) {
    for (register int i = 0; i < pf; ++i) {
      int to = (q[hq] + f[i]) % k;
      if (dis[k - to])
        return printf("%d\n", dis[q[hq]] + dis[k - to] + 1), 0;
      if (!vis[to])
        q[tq++] = to, dis[to] = dis[q[hq]] + 1, vis[to] = 1;
    }
  }
  return 0;
}