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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#ifndef ONLINE_JUDGE
const bool DEBUG = true;
#else
const bool DEBUG = false;
#endif

// Reads a 1D vector
template <class T> void read1d(vector<T> &arg) {
  const int l = arg.size();
  for (int i = 0; i < l; ++i)
    cin >> arg[i];
}

void read() {}
// Reads arbitrary values
template <class T1, class... T2> void read(T1 &var1, T2 &...var2) {
  cin >> var1;
  read(var2...);
}

void pln() { cout << (DEBUG ? "]\\n" : "\\n"); }
template <class T1, class... T2> void pln(const T1 &var1, const T2 &...var2) {
  cout << var1 << " ";
  pln(var2...);
}

// Prints arbitrary values and goes to next line
template <class T1, class... T2>
constexpr void print(const T1 &var1, const T2 &...var2) {
  cout << (DEBUG ? " [ " : "");
  pln(var1, var2...);
}

// Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE
template <class T1, class... T2>
constexpr void debug(const T1 &var1, const T2 &...var2) {
  if (!DEBUG)
    return;
  cout << " debug : [ ";
  pln(var1, var2...);
}

using ll = long long;
using ull = unsigned long long;

#define all(x) begin(x), end(x)

const bool MULTIPLE_TEST_CASES = 0;

void exec() {
  int n;
  read(n);
  if (n % 2 == 0 || n == 5) {
    print(-1);
    return;
  }

  if (n % 7 == 0)
    n /= 7;

  int m = 1 % n;
  int ans = 1;
  int tenmod = 10 % n;
  int prev_ten = 1;

  unordered_set<int> remainders;
  remainders.insert(m);

  while (m != 0) {
    prev_ten = (prev_ten * tenmod) % n;
    m = (m + prev_ten) % n;

    if (remainders.count(m)) {
      print(-1);
      return;
    }
    remainders.insert(m);

    ++ans;
  }

  print(ans);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  if (!MULTIPLE_TEST_CASES)
    exec();
  else {
    int n;
    cin >> n;

    while (n--)
      exec();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#ifndef ONLINE_JUDGE
const bool DEBUG = true;
#else
const bool DEBUG = false;
#endif

// Reads a 1D vector
template <class T> void read1d(vector<T> &arg) {
  const int l = arg.size();
  for (int i = 0; i < l; ++i)
    cin >> arg[i];
}

void read() {}
// Reads arbitrary values
template <class T1, class... T2> void read(T1 &var1, T2 &...var2) {
  cin >> var1;
  read(var2...);
}

void pln() { cout << (DEBUG ? "]\\n" : "\\n"); }
template <class T1, class... T2> void pln(const T1 &var1, const T2 &...var2) {
  cout << var1 << " ";
  pln(var2...);
}

// Prints arbitrary values and goes to next line
template <class T1, class... T2>
constexpr void print(const T1 &var1, const T2 &...var2) {
  cout << (DEBUG ? " [ " : "");
  pln(var1, var2...);
}

// Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE
template <class T1, class... T2>
constexpr void debug(const T1 &var1, const T2 &...var2) {
  if (!DEBUG)
    return;
  cout << " debug : [ ";
  pln(var1, var2...);
}

using ll = long long;
using ull = unsigned long long;

#define all(x) begin(x), end(x)

const bool MULTIPLE_TEST_CASES = 0;

void exec() {
  int n;
  read(n);
  if (n % 2 == 0 || n == 5) {
    print(-1);
    return;
  }

  if (n % 7 == 0)
    n /= 7;

  int m = 1 % n;
  int ans = 1;
  int tenmod = 10 % n;
  int prev_ten = 1;

  while (m != 0) {
    prev_ten = (prev_ten * tenmod) % n;
    m = (m + prev_ten) % n;
    ++ans;
  }

  print(ans);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  if (!MULTIPLE_TEST_CASES)
    exec();
  else {
    int n;
    cin >> n;

    while (n--)
      exec();
  }

  return 0;
}