Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  ll k;
  cin >> k;
  ll now = 0;
  int ans = 0;
  while (true) {
    bool ok = false;
    rep(i, 10) {
      ll tmp = now + k * i;
      if (tmp % 10 == 7) {
        swap(tmp, now);
        now /= 10;
        ans++;
        ok = true;
        break;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    if (now == 0)
      break;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  ll k;
  cin >> k;
  int mul = k % 10;
  ll now = 0;
  int ans = 0;
  while (true) {
    int x = k % 10;
    int rem = now % 10;
    bool ok = false;
    rep(i, 10) {
      ll tmp = now + k * i;
      if (tmp % 10 == 7) {
        swap(tmp, now);
        now /= 10;
        ans++;
        ok = true;
        break;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    if (now == 7) {
      ans++;
      break;
    }
  }
  cout << ans << endl;
}