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

#include <algorithm> // sort, 二分探索, etc
#include <cmath>
#include <iomanip> // setprecision (浮動小数点の出力の誤差)
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define cauto const auto &
#define UNIQUE(v) (v).erase( unique(all(v), (v).end() );
#define SZ(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define INF 1000000000000
#define MOD 1000000007
#define perm(c)                                                                \\
  sort(all(c));                                                                \\
  for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))

using namespace std;
using ll = long long;
using ull = unsigned long long;

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

const char newl = '\\n';

ll qp(ll a, ull b) {
  ll ans = 1;
  do {
    if (b & 1u)
      ans *= a;
    a *= a;
  } while (b >>= 1u);
  return ans;
}
ll qp(ll a, ull b, ll mod) {
  ll ans = 1;
  do {
    if (b & 1u)
      ans = ans * a % mod;
    a = a * a % mod;
  } while (b >>= 1u);
  return ans;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll comb(int n, int r) {
  vector<vector<ll>> v(n + 1, vector<ll>(r + 1, 0));
  for (int i = 0; i < n + 1; i++) {
    v[i][0] = 1;
    if (i < r + 1)
      v[i][i] = 1;
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < min(i, r + 1); j++) {
      v[i][j] = v[i - 1][j - 1] + v[i - 1][j];
    }
  }
  return v[n][r];
}

// INT_MAX   = 2.1e+09 (2147483647)
// LONG_MAX  = 9.2e+18 (9223372036854775807)
// LLONG_MAX = 9.2e+18 (9223372036854775807)

int main() {
  ll k;
  cin >> k;

  ll k2 = (k % 7 == 0) ? k / 7 : k;
  if (k2 == 1) {
    cout << 1 << newl;
    return 0;
  }
  if (k2 % 2 == 0) {
    cout << -1 << newl;
    return 0;
  }

  ll total = 1;
  ll r = 1;
  ll cnt = 1;
  map<pair<ll, ll>, ll> mp;
  while (true) {
    if (mp[pair<ll, ll>(r, total)] > 0) {
      cout << -1 << newl;
      return 0;
    } else {
      mp[pair<ll, ll>(r, total)] += 1;
    }
    r = (r * 10) % k2;
    total = (total + r) % k2;
    if (total == 0) {
      cout << cnt + 1 << newl;
      return 0;
    }
    cnt++;
  }
}


Error: TLE

Fixed submission:

#include <algorithm> // sort, 二分探索, etc
#include <cmath>
#include <iomanip> // setprecision (浮動小数点の出力の誤差)
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define cauto const auto &
#define UNIQUE(v) (v).erase( unique(all(v), (v).end() );
#define SZ(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define INF 1000000000000
#define MOD 1000000007
#define perm(c)                                                                \\
  sort(all(c));                                                                \\
  for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))

using namespace std;
using ll = long long;
using ull = unsigned long long;

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

const char newl = '\\n';

ll qp(ll a, ull b) {
  ll ans = 1;
  do {
    if (b & 1u)
      ans *= a;
    a *= a;
  } while (b >>= 1u);
  return ans;
}
ll qp(ll a, ull b, ll mod) {
  ll ans = 1;
  do {
    if (b & 1u)
      ans = ans * a % mod;
    a = a * a % mod;
  } while (b >>= 1u);
  return ans;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll comb(int n, int r) {
  vector<vector<ll>> v(n + 1, vector<ll>(r + 1, 0));
  for (int i = 0; i < n + 1; i++) {
    v[i][0] = 1;
    if (i < r + 1)
      v[i][i] = 1;
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < min(i, r + 1); j++) {
      v[i][j] = v[i - 1][j - 1] + v[i - 1][j];
    }
  }
  return v[n][r];
}

// INT_MAX   = 2.1e+09 (2147483647)
// LONG_MAX  = 9.2e+18 (9223372036854775807)
// LLONG_MAX = 9.2e+18 (9223372036854775807)

int main() {
  ll k;
  cin >> k;

  ll k2 = (k % 7 == 0) ? k / 7 : k;
  if (k2 == 1) {
    cout << 1 << newl;
    return 0;
  }
  if (k2 % 2 == 0) {
    cout << -1 << newl;
    return 0;
  }

  ll total = 1;
  ll r = 1;
  ll cnt = 1;
  while (true) {
    r = (r * 10) % k2;
    total = (total + r) % k2;
    if (total == 0) {
      cout << cnt + 1 << newl;
      return 0;
    }
    cnt++;
  }
}