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

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

#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)

#define MOD 1000000007

#define FOR(a, c) for (ll(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define FORR(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)

#define INF 9223372036854775807
// 18446744073709551615
#define abs llabs
#define by_ANIKET                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define no cout << "No\\n";
#define yes cout << "Yes\\n";

#define Clear(a, b) memset(a, b, sizeof(a))
#define run                                                                    \\
  if (1)                                                                       \\
    cout << __LINE__ << endl;

#define pr3(a, b, c)                                                           \\
  cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\t" << #c << "=" << c   \\
       << "\\n";
#define pr2(a, b) cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\n";
#define pr1(a) cerr << #a << "=" << a << "\\n";
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair

inline long long max3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}

ll powm(ll x, ll y, ll m = MOD) {
  x = x % m;
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}
ll modFact(ll n, ll p = MOD) {
  if (n >= p)
    return 0;

  ll result = 1;
  for (ll i = 1; i <= n; i++)
    result = (result * i) % p;

  return result;
}
void solve() {
  ll n, m, t = 0, k = 0, x = 0, y = 0, z = 0, p, a1, a2, a3, a4, a5, ex, che,
           var = 1, yd, f = INF, templ, tempr;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1\\n";
    return;
  }
  n = k;
  while (n) {
    n /= 10;
    y = y * 10 + 7;
    x++;
  }
  // pr2(x,y)
  set<ll> don;
  a1 = y % k;
  don.insert(a1);
  while (a1) {
    y = a1;
    y = y * 10 + 7;
    x++;
    a1 = y % k;
    if (don.find(a1) != don.end()) {
      cout << "-1\\n";
      return;
    }
  }
  cout << x << "\\n";
}

int main() {
  by_ANIKET ll T = 1, t = 0;
  // cin>>T;
  while (t++ < T) {
    // cout<<"Case "<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}
//   ©
//                      ______  __    __ ______ __    __ ________ ________
//                     /      \\|  \\  |  \\      \\  \\  /  \\        \\        \\
//                    |  ▓▓▓▓▓▓\\ ▓▓\\ | ▓▓\\▓▓▓▓▓▓ ▓▓ /  ▓▓ ▓▓▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓
//  ______ ______     | ▓▓__| ▓▓ ▓▓▓\\| ▓▓ | ▓▓ | ▓▓/  ▓▓| ▓▓__      | ▓▓
// |      \\      \\    | ▓▓    ▓▓ ▓▓▓▓\\ ▓▓ | ▓▓ | ▓▓  ▓▓ | ▓▓  \\     | ▓▓
//  \\▓▓▓▓▓▓\\▓▓▓▓▓▓    | ▓▓▓▓▓▓▓▓ ▓▓\\▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓\\ | ▓▓▓▓▓     | ▓▓
//                    | ▓▓  | ▓▓ ▓▓ \\▓▓▓▓_| ▓▓_| ▓▓ \\▓▓\\| ▓▓_____   | ▓▓
//                    | ▓▓  | ▓▓ ▓▓  \\▓▓▓   ▓▓ \\ ▓▓  \\▓▓\\ ▓▓     \\  | ▓▓
//                     \\▓▓   \\▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓▓▓   \\▓▓

Error: TLE

Fixed submission:

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

#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)

#define MOD 1000000007

#define FOR(a, c) for (ll(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define FORR(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)

#define INF 9223372036854775807
// 18446744073709551615
#define abs llabs
#define by_ANIKET                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define no cout << "No\\n";
#define yes cout << "Yes\\n";

#define Clear(a, b) memset(a, b, sizeof(a))
#define run                                                                    \\
  if (1)                                                                       \\
    cout << __LINE__ << endl;

#define pr3(a, b, c)                                                           \\
  cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\t" << #c << "=" << c   \\
       << "\\n";
#define pr2(a, b) cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\n";
#define pr1(a) cerr << #a << "=" << a << "\\n";
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair

inline long long max3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}

ll powm(ll x, ll y, ll m = MOD) {
  x = x % m;
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}
ll modFact(ll n, ll p = MOD) {
  if (n >= p)
    return 0;

  ll result = 1;
  for (ll i = 1; i <= n; i++)
    result = (result * i) % p;

  return result;
}
void solve() {
  ll n, m, t = 0, k = 0, x = 0, y = 0, z = 0, p, a1, a2, a3, a4, a5, ex, che,
           var = 1, yd, f = INF, templ, tempr;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1\\n";
    return;
  }
  n = k;
  while (n) {
    n /= 10;
    y = y * 10 + 7;
    x++;
  }
  // pr2(x,y)
  a1 = y % k;
  while (a1) {
    y = a1;
    y = y * 10 + 7;
    x++;
    a1 = y % k;
  }
  cout << x << "\\n";
}

int main() {
  by_ANIKET ll T = 1, t = 0;
  // cin>>T;
  while (t++ < T) {
    // cout<<"Case "<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}
//   ©
//                      ______  __    __ ______ __    __ ________ ________
//                     /      \\|  \\  |  \\      \\  \\  /  \\        \\        \\
//                    |  ▓▓▓▓▓▓\\ ▓▓\\ | ▓▓\\▓▓▓▓▓▓ ▓▓ /  ▓▓ ▓▓▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓
//  ______ ______     | ▓▓__| ▓▓ ▓▓▓\\| ▓▓ | ▓▓ | ▓▓/  ▓▓| ▓▓__      | ▓▓
// |      \\      \\    | ▓▓    ▓▓ ▓▓▓▓\\ ▓▓ | ▓▓ | ▓▓  ▓▓ | ▓▓  \\     | ▓▓
//  \\▓▓▓▓▓▓\\▓▓▓▓▓▓    | ▓▓▓▓▓▓▓▓ ▓▓\\▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓\\ | ▓▓▓▓▓     | ▓▓
//                    | ▓▓  | ▓▓ ▓▓ \\▓▓▓▓_| ▓▓_| ▓▓ \\▓▓\\| ▓▓_____   | ▓▓
//                    | ▓▓  | ▓▓ ▓▓  \\▓▓▓   ▓▓ \\ ▓▓  \\▓▓\\ ▓▓     \\  | ▓▓
//                     \\▓▓   \\▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓▓▓   \\▓▓