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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// 桁数
int cKeta(i64 v) {
  int r = 0;
  while (v) {
    v /= 10;
    r++;
  }
  return r;
}

void calc(int K) {
  if ((K & 1) == 0 || (K % 5) == 0) {
    cout << -1;
  } else {
    int k = cKeta(K);
    i64 d = 7;

    int res = 0;
    while (d) {
      res++;
      i64 r = d % K;
      if (!r) {
        break;
      }
      // debug(res);
      // cout << d << ' ' << r << newl;
      d = r * 10 + 7;
    }
    cout << res;
  }
  cout << newl;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

#if 1
  int K;
  cin >> K;
  calc(K);

#else
  FOR(i, 1, 1000001) {
    debug(i);
    calc(i);
  }
#endif
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// 桁数
int cKeta(i64 v) {
  int r = 0;
  while (v) {
    v /= 10;
    r++;
  }
  return r;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int K;
  cin >> K;
  if ((K & 1) == 0) {
    cout << -1;
  } else {
    int k = cKeta(K);
    i64 d = 7;

    int res = 0;
    while (d) {
      res++;
      i64 r = d % K;
      if (!r) {
        break;
      }
      // debug(res);
      // cout << d << ' ' << r << newl;
      d = r * 10 + 7;
    }
    cout << res;
  }
}
#endif