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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define D(a) (double)(a)
#define vct vector
#define vi vct<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test" << endl;
#define read(v, a, n)                                                          \\
  for (int i = a; i < n; i++)                                                  \\
    cin >> v[i];
#define For(a, n, in) for (int i = a; i < n; i += in)
#define print(v)                                                               \\
  for (auto it : v)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define PI acos(-1)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define t_c                                                                    \\
  int test, cs = 1;                                                            \\
  cin >> test;                                                                 \\
  while (test--)
///................function.....................///
#define siz(s) (int)(s.size())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mod 999999937
int main() {
  /// freopen("in.txt","r",stdin);
  /// freopen("output.txt","w",stdout);
  ll k;
  cin >> k;
  int res = 0, cnt = 0;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  while (1) {
    res = res * 10 + 7;
    cnt++;
    if (res % k == 0)
      break;
    res %= k;
  }
  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define D(a) (double)(a)
#define vct vector
#define vi vct<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test" << endl;
#define read(v, a, n)                                                          \\
  for (int i = a; i < n; i++)                                                  \\
    cin >> v[i];
#define For(a, n, in) for (int i = a; i < n; i += in)
#define print(v)                                                               \\
  for (auto it : v)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define PI acos(-1)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define t_c                                                                    \\
  int test, cs = 1;                                                            \\
  cin >> test;                                                                 \\
  while (test--)
///................function.....................///
#define siz(s) (int)(s.size())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mod 999999937
int main() {
  /// freopen("in.txt","r",stdin);
  /// freopen("output.txt","w",stdout);
  ll k;
  cin >> k;
  int res = 0, cnt = 0;
  while (1) {
    res = res * 10 + 7;
    cnt++;
    if (res % k == 0)
      break;
    res %= k;
  }
  cout << cnt << endl;
}