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

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define st first
#define nd second
#define endl '\\n'
#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x)                                                           \\
  cerr << #x << " is ";                                                        \\
  for (auto &e : (x))                                                          \\
    cerr << e << ' ';                                                          \\
  cerr << '\\n'; // vector, set
#define what_is_a(x, n)                                                        \\
  cerr << #x << " is ";                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    cerr << x[i] << ' ';                                                       \\
  cerr << '\\n'; // n first element of array
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x)                                                          \\
  cerr << #x << " is ";                                                        \\
  for (auto &e : (x))                                                          \\
    cerr << cerr_pair(e) << ' ';                                               \\
  cerr << '\\n'; // vector, set
#define pwhat_is_a(x, n)                                                       \\
  cerr << #x << " is ";                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    cerr << cerr_pair(x[i]) << ' ';                                            \\
  cerr << '\\n'; // n first element of array
// #define int long long
// #define pow mypow

typedef pair<int, int> pii;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;

int k;
int32_t main() {
  IOS
          // freopen("input.txt", "r", stdin);
          cin >>
      k;
  if (k % 7 == 0)
    k /= 7;
  if (__gcd(10, k) != 1)
    return 0 * puts("-1");
  int ans = 1;
  int r = 1 % k;
  while (r != 0) {
    r = (10 * r + 1) % k;
    ans++;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define st first
#define nd second
#define endl '\\n'
#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x)                                                           \\
  cerr << #x << " is ";                                                        \\
  for (auto &e : (x))                                                          \\
    cerr << e << ' ';                                                          \\
  cerr << '\\n'; // vector, set
#define what_is_a(x, n)                                                        \\
  cerr << #x << " is ";                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    cerr << x[i] << ' ';                                                       \\
  cerr << '\\n'; // n first element of array
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x)                                                          \\
  cerr << #x << " is ";                                                        \\
  for (auto &e : (x))                                                          \\
    cerr << cerr_pair(e) << ' ';                                               \\
  cerr << '\\n'; // vector, set
#define pwhat_is_a(x, n)                                                       \\
  cerr << #x << " is ";                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    cerr << cerr_pair(x[i]) << ' ';                                            \\
  cerr << '\\n'; // n first element of array
// #define int long long
// #define pow mypow

typedef pair<int, int> pii;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;

int k;
int32_t main() {
  IOS
          // freopen("input.txt", "r", stdin);
          cin >>
      k;
  if (k % 7 == 0)
    k /= 7;
  int ans = 1;
  int r = 1 % k;
  while (r != 0) {
    r = (10 * r + 1) % k;
    ans++;
  }
  cout << ans << endl;

  return 0;
}