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

#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl '\\n'
#ifndef CODE_JAM
#undef CASE_INFO
#endif
#else
#pragma GCC optimize "trapv"
#endif
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define all(p) p.begin(), p.end()
#define count_1(p) __builtin_popcountll(p)
#define count_0(p) __builtin_ctzll(p)
using namespace std;
template <class X = int> inline X mid(X s, X e) { return (s + (e - s) / 2); }
template <class X = int> inline X len(X s, X e) { return (e - s + 1); }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int oo = 987654321;
const ll OO = 1e18;

class Timer {
public:
  clock_t start;
  Timer() { start = clock(); }
  ~Timer() { cerr << (double)(clock() - start) / CLOCKS_PER_SEC << "\\n"; }
};

void solve() {
  int k;
  cin >> k;
  set<int> s;
  int cur = 7 % k;
  int steps = 1;
  s.insert(cur);
  while (1) {
    if (cur == 0) {
      cout << steps << endl;
      return;
    }
    cur = ((cur * 10) + 7) % k;
    if (cur != 0 and s.count(cur)) {
      cout << "-1\\n";
      return;
    }
    s.insert(cur);
    steps++;
  }
}

signed main() {
  Timer ti;
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
#ifdef TEST_CASES
  cin >> T;
#endif
  for (int t = 1; t <= T; t++) {
#ifdef CASE_INFO
    cout << "Case #" << t << ": ";
#endif
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl '\\n'
#ifndef CODE_JAM
#undef CASE_INFO
#endif
#else
#pragma GCC optimize "trapv"
#endif
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define all(p) p.begin(), p.end()
#define count_1(p) __builtin_popcountll(p)
#define count_0(p) __builtin_ctzll(p)
using namespace std;
template <class X = int> inline X mid(X s, X e) { return (s + (e - s) / 2); }
template <class X = int> inline X len(X s, X e) { return (e - s + 1); }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int oo = 987654321;
const ll OO = 1e18;

class Timer {
public:
  clock_t start;
  Timer() { start = clock(); }
  ~Timer() { cerr << (double)(clock() - start) / CLOCKS_PER_SEC << "\\n"; }
};

void solve() {
  int k;
  cin >> k;
  set<int> s;
  int cur = 7 % k;
  int steps = 1;
  s.insert(cur);
  while (1) {
    if (cur == 0) {
      cout << steps << endl;
      return;
    }
    cur = ((cur * 10) + 7) % k;
    if (cur != 0 and s.count(cur)) {
      cout << "-1\\n";
      return;
    }
    steps++;
  }
}

signed main() {
  Timer ti;
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
#ifdef TEST_CASES
  cin >> T;
#endif
  for (int t = 1; t <= T; t++) {
#ifdef CASE_INFO
    cout << "Case #" << t << ": ";
#endif
    solve();
  }
  return 0;
}