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

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define fi first
#define se second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}

template <typename T> inline void remax(T &x, T y) { x = max(x, y); }

template <typename T> inline void remin(T &x, T y) { x = min(x, y); }

constexpr int ms = 1e6 + 13;

int fd[ms];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  if (n % 2 == 0)
    cout << -1 << '\\n';
  else {
    int cur_mod = (7 % n);
    int cur_v = 0;
    fd[cur_mod] = 1;
    while (cur_mod != 0) {
      cur_mod = (cur_mod * 10 + 7) % n;
      cur_v++;
      if (fd[cur_mod] == 1)
        break;
      fd[cur_mod] = 1;
    }
    if (cur_mod == 0)
      cout << cur_v + 1 << '\\n';
    else
      cout << -1 << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define fi first
#define se second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}

template <typename T> inline void remax(T &x, T y) { x = max(x, y); }

template <typename T> inline void remin(T &x, T y) { x = min(x, y); }

constexpr int ms = 1e6 + 13;

int fd[ms];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  if (n % 2 == 0)
    cout << -1 << '\\n';
  else {
    int cur_mod = (7 % n);
    int cur_v = 0;
    fd[cur_mod] = 1;
    while (cur_mod != 0) {
      int lg_mod = (cur_mod * 10 + 7) % n;
      cur_v++;
      if (fd[lg_mod] == 1)
        break;
      cur_mod = lg_mod;
    }
    if (cur_mod == 0)
      cout << cur_v + 1 << '\\n';
    else
      cout << -1 << '\\n';
  }
  return 0;
}