Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int k;
  cin >> k;
  if (k == 7 || k == 1) {
    cout << 1 << endl;
    return 0;
  }
  int d = 7;
  int r = 0;
  int res = 0;
  do {
    if (d == 0) {
      cout << -1 << endl;
      return 0;
    }
    r += d % k;
    ++res;
    r %= k;
    d *= 10;
    d %= k;
  } while (r != 0);
  cout << res << endl;
}

Fixed submission:
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int k;
  cin >> k;
  if (k == 7 || k == 1) {
    cout << 1 << endl;
    return 0;
  }
  int d = 7;
  int r = 0;
  int res = 0;
  do {
    if (d == 0 || res > 1e8) {
      cout << -1 << endl;
      return 0;
    }
    r += d % k;
    ++res;
    r %= k;
    d *= 10;
    d %= k;
  } while (r != 0);
  cout << res << endl;
}