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

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右下左上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, 1};

namespace {
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// 標準出力
void Write(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Write(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T, typename U>
void Write(const T &x, const U &y, const char *end = "\\n") {
  std::cout << x << " " << y << end;
}
template <typename T, typename U, typename V>
void Write(const T &x, const U &y, const V &z, const char *end = "\\n") {
  std::cout << x << " " << y << " " << z << end;
}
// template <typename... Args> void Write(Args const&... args) { bool a = true;
// for (auto const& x : { args... }) { if (a) a = false; else std::cout << " ";
// std::cout << x; } std::cout << "\\n"; }
template <typename T>
void Write(const std::vector<T> &x, const char *sep = " ",
           const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; ++i) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}
template <typename T>
void Write(const std::vector<std::vector<T>> &x, const char *sep = " ",
           const char *end = "\\n") {
  for (auto v : x)
    Write(v, sep, end);
}

// 標準入力
struct Read {
  std::size_t szi, szj;
  Read(std::size_t _szi = 1, std::size_t _szj = 1) : szi(_szi), szj(_szj) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(szi);
    for (std::size_t i = 0; i < szi; ++i)
      std::cin >> a[i];
    return a;
  }
  template <typename T> operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
  template <typename T, typename U>
  operator std::vector<std::pair<T, U>>() const {
    std::vector<std::pair<T, U>> a(szi);
    for (std::size_t i = 0; i < szi; ++i)
      std::cin >> a[i].first >> a[i].second;
    return a;
  }
};
struct Reads {
  template <typename T> Reads(T &a) { std::cin >> a; }
  template <typename T, typename U> Reads(T &a, U &b) { std::cin >> a >> b; }
  template <typename T, typename U, typename V> Reads(T &a, U &b, V &c) {
    std::cin >> a >> b >> c;
  }
  template <typename T, typename U, typename V, typename W>
  Reads(T &a, U &b, V &c, W &d) {
    std::cin >> a >> b >> c >> d;
  }
};

Read read;
} // namespace

int main() {

  int k = read;

  int remain = 7 % k;

  map<int, bool> mp;
  mp[remain] = true;
  int cnt = 1;
  int ans = 1;

  while (remain != 0) {
    remain = remain * 10 + 7;
    remain %= k;
    if (mp[remain]) {
      ans = -1;
      break;
    }
    mp[remain] = true;
    ans = ++cnt;
  }

  Write(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右下左上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, 1};

namespace {
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// 標準出力
void Write(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Write(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T, typename U>
void Write(const T &x, const U &y, const char *end = "\\n") {
  std::cout << x << " " << y << end;
}
template <typename T, typename U, typename V>
void Write(const T &x, const U &y, const V &z, const char *end = "\\n") {
  std::cout << x << " " << y << " " << z << end;
}
// template <typename... Args> void Write(Args const&... args) { bool a = true;
// for (auto const& x : { args... }) { if (a) a = false; else std::cout << " ";
// std::cout << x; } std::cout << "\\n"; }
template <typename T>
void Write(const std::vector<T> &x, const char *sep = " ",
           const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; ++i) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}
template <typename T>
void Write(const std::vector<std::vector<T>> &x, const char *sep = " ",
           const char *end = "\\n") {
  for (auto v : x)
    Write(v, sep, end);
}

// 標準入力
struct Read {
  std::size_t szi, szj;
  Read(std::size_t _szi = 1, std::size_t _szj = 1) : szi(_szi), szj(_szj) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(szi);
    for (std::size_t i = 0; i < szi; ++i)
      std::cin >> a[i];
    return a;
  }
  template <typename T> operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
  template <typename T, typename U>
  operator std::vector<std::pair<T, U>>() const {
    std::vector<std::pair<T, U>> a(szi);
    for (std::size_t i = 0; i < szi; ++i)
      std::cin >> a[i].first >> a[i].second;
    return a;
  }
};
struct Reads {
  template <typename T> Reads(T &a) { std::cin >> a; }
  template <typename T, typename U> Reads(T &a, U &b) { std::cin >> a >> b; }
  template <typename T, typename U, typename V> Reads(T &a, U &b, V &c) {
    std::cin >> a >> b >> c;
  }
  template <typename T, typename U, typename V, typename W>
  Reads(T &a, U &b, V &c, W &d) {
    std::cin >> a >> b >> c >> d;
  }
};

Read read;
} // namespace

int main() {

  int k = read;

  int remain = 7 % k;

  int start = remain;
  int cnt = 1;
  int ans = 1;

  while (remain != 0) {
    remain = remain * 10 + 7;
    remain %= k;
    if (remain == start) {
      ans = -1;
      break;
    }
    ans = ++cnt;
  }

  Write(ans);

  return 0;
}