Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \
  for (auto i : A)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl
#define err4(a, b, c, d)                                                       \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 100005;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll k;
  cin >> k;
  ll p = 1;
  ll rem = 0;
  if (k % 2 == 0) {
    cout << -1 << endl;
  } else {
    ll cnt = 0;
    while (1) {
      cnt++;
      rem = (rem + p * 7) % k;
      p = (p * 10) % k;
      if (rem == 0) {
        cout << cnt << endl;
        return 0;
      }
    }
  }
  return 0;
}

Fixed submission:
// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \
  for (auto i : A)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl
#define err4(a, b, c, d)                                                       \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 100005;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll k;
  cin >> k;
  ll p = 1;
  ll rem = 0;
  if (k % 2 == 0 or k % 5 == 0) {
    cout << -1 << endl;
  } else {
    ll cnt = 0;
    while (1) {
      cnt++;
      rem = (rem + p * 7) % k;
      p = (p * 10) % k;
      if (rem == 0) {
        cout << cnt << endl;
        return 0;
      }
    }
  }
  return 0;
}