Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long
#define endl '\n'
#define pb push_back
#define debug(x) cout << #x << " " << x << endl;
const int mod = 1e18 + 7;
const int MAX = 1e5 + 9;
const double pi = 3.141592653589793238460;
using namespace std;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

bool is_pal(string s) {
  return equal(s.begin(), s.begin() + s.size() / 2, s.rbegin()) ? 1 : 0;
}

int power(int x, int y, int res = 1) {
  x %= mod;
  for (; y; y >>= 1LL) {
    if (y & 1LL) {
      res = res * x % mod;
    }
    x = x * x % mod;
  }
  return res;
}

int modInv(int x) { return (power(x, mod - 2) + mod) % mod; }

int fact[MAX], inv[MAX];

void compFact(void) {
  fact[0] = fact[1] = inv[1] = inv[0] = 1;
  for (int i = 1; i < MAX; i++) {
    fact[i] = (fact[i - 1] * i % mod + mod) % mod;
  }
}

void compInv(void) {
  for (int i = 2; i < MAX; ++i) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod + mod) % mod;
  }
}

int nCr(int n, int r) {
  if (n < r || n < 0) {
    return 0;
  }
  return ((((fact[n] % mod) * (inv[fact[n - r]] % mod)) % mod) *
              (inv[fact[r]] % mod) +
          mod) %
         mod;
}

int factMod(int n, int res = 1) {
  while (n > 1) {
    res = (res * ((n / mod) % 2 ? mod - 1 : 1)) % mod;
    for (int i = 2; i <= n % mod; ++i)
      res = (res * i) % mod;
    n /= mod;
  }
  return res % mod;
}

int max(int x, int y) { return x > y ? x : y; }

int min(int x, int y) { return x < y ? x : y; }

int lcm(int x, int y) { return (x * y) / __gcd(x, y); }

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int N;

bool under_bound(int x, int y) { return x >= 0 && y <= 0 && x < N && y < N; }

bool prime[MAX];

void SieveOfEratosthenes(int n) {

  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p < n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}

multiset<int> A;

void primeFact(int n) {
  while (n % 2 == 0) {
    A.insert(2);
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      A.insert(i);
      n = n / i;
    }
  }

  if (n > 2)
    A.insert(n);
}

int spf[MAX];

void SieveOfEuler() {
  spf[1] = 1;

  for (int i = 2; i < MAX; i++)
    spf[i] = i;

  for (int i = 4; i < MAX; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAX; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAX; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

map<pair<int, int>, int> factFreq;

void getFactorization(int x) {
  while (x > 1) {
    int pt = spf[x], f = 0;
    while (x % pt == 0) {
      x /= pt;
      f++;
      factFreq[{pt, f}]++;
    }
  }
}

int n, k, z;
int a[MAX];
map<vector<int>, int> save;

int go(int pos, int left, int now) {
  if (now == -1) {
    return 0;
  }

  vector<int> key = {pos, left, now};

  if (save[key] != NULL)
    return save[key];

  int ans = 0;
  if (pos == 1) {
    ans += (a[pos] + go(pos + 1, left, now - 1));
  } else if (pos == n) {
    if (left > 0)
      ans += max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
    else
      return a[pos];
  } else {
    if (left > 0)
      ans = max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
    ans = max(ans, a[pos] + go(pos + 1, left, now - 1));
  }
  return save[key] = ans;
}

int ans = 1;

void solve(void) {
  int n;
  cin >> n;
  if (!(n & 1)) {
    cout << -1;
    return;
  }

  int now = 7;

  while (now % n != 0) {
    now = now * 10 + 7;
    now %= n;
    ans++;
  }

  cout << ans;
}
signed main() {
  fio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long
#define endl '\n'
#define pb push_back
#define debug(x) cout << #x << " " << x << endl;
const int mod = 1e18 + 7;
const int MAX = 1e5 + 9;
const double pi = 3.141592653589793238460;
using namespace std;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

bool is_pal(string s) {
  return equal(s.begin(), s.begin() + s.size() / 2, s.rbegin()) ? 1 : 0;
}

int power(int x, int y, int res = 1) {
  x %= mod;
  for (; y; y >>= 1LL) {
    if (y & 1LL) {
      res = res * x % mod;
    }
    x = x * x % mod;
  }
  return res;
}

int modInv(int x) { return (power(x, mod - 2) + mod) % mod; }

int fact[MAX], inv[MAX];

void compFact(void) {
  fact[0] = fact[1] = inv[1] = inv[0] = 1;
  for (int i = 1; i < MAX; i++) {
    fact[i] = (fact[i - 1] * i % mod + mod) % mod;
  }
}

void compInv(void) {
  for (int i = 2; i < MAX; ++i) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod + mod) % mod;
  }
}

int nCr(int n, int r) {
  if (n < r || n < 0) {
    return 0;
  }
  return ((((fact[n] % mod) * (inv[fact[n - r]] % mod)) % mod) *
              (inv[fact[r]] % mod) +
          mod) %
         mod;
}

int factMod(int n, int res = 1) {
  while (n > 1) {
    res = (res * ((n / mod) % 2 ? mod - 1 : 1)) % mod;
    for (int i = 2; i <= n % mod; ++i)
      res = (res * i) % mod;
    n /= mod;
  }
  return res % mod;
}

int max(int x, int y) { return x > y ? x : y; }

int min(int x, int y) { return x < y ? x : y; }

int lcm(int x, int y) { return (x * y) / __gcd(x, y); }

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int N;

bool under_bound(int x, int y) { return x >= 0 && y <= 0 && x < N && y < N; }

bool prime[MAX];

void SieveOfEratosthenes(int n) {

  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p < n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}

multiset<int> A;

void primeFact(int n) {
  while (n % 2 == 0) {
    A.insert(2);
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      A.insert(i);
      n = n / i;
    }
  }

  if (n > 2)
    A.insert(n);
}

int spf[MAX];

void SieveOfEuler() {
  spf[1] = 1;

  for (int i = 2; i < MAX; i++)
    spf[i] = i;

  for (int i = 4; i < MAX; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAX; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAX; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

map<pair<int, int>, int> factFreq;

void getFactorization(int x) {
  while (x > 1) {
    int pt = spf[x], f = 0;
    while (x % pt == 0) {
      x /= pt;
      f++;
      factFreq[{pt, f}]++;
    }
  }
}

int n, k, z;
int a[MAX];
map<vector<int>, int> save;

int go(int pos, int left, int now) {
  if (now == -1) {
    return 0;
  }

  vector<int> key = {pos, left, now};

  if (save[key] != NULL)
    return save[key];

  int ans = 0;
  if (pos == 1) {
    ans += (a[pos] + go(pos + 1, left, now - 1));
  } else if (pos == n) {
    if (left > 0)
      ans += max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
    else
      return a[pos];
  } else {
    if (left > 0)
      ans = max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
    ans = max(ans, a[pos] + go(pos + 1, left, now - 1));
  }
  return save[key] = ans;
}

int ans = 1;

void solve(void) {
  int n;
  cin >> n;
  if (!(n & 1) || n % 5 == 0) {
    cout << -1;
    return;
  }

  int now = 7;

  while (now % n != 0) {
    now = now * 10 + 7;
    now %= n;
    ans++;
  }

  cout << ans;
}
signed main() {
  fio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}