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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, n) for (ll i = (a); i < (n); ++i)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define All(a) (a).begin(), (a).end()
#define Pi acos(-1)
using Graph = vector<vector<ll>>;

class mint {
public:
  long long x;
  constexpr mint(long long x = 0) : x((x % mod + mod) % mod) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  constexpr mint inv() const { return pow(mod - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
};
ostream &operator<<(ostream &os, const mint &m) {
  os << m.x;
  return os;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  ll k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << '\\n';
    return 0;
  }
  ll s = 7;
  for (ll i = 1; i < k + 100; ++i) {
    if (s % k == 0) {
      cout << i << '\\n';
      return 0;
    }
    s += 7 * modpow(10, i, k);
    s %= k;
  }
  cout << -1 << '\\n';

  end = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<chrono::milliseconds>(end - start).count();
  cerr << elapsed << "ms" << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, n) for (ll i = (a); i < (n); ++i)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#define All(a) (a).begin(), (a).end()
#define Pi acos(-1)
using Graph = vector<vector<ll>>;

class mint {
public:
  long long x;
  constexpr mint(long long x = 0) : x((x % mod + mod) % mod) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  constexpr mint inv() const { return pow(mod - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
};
ostream &operator<<(ostream &os, const mint &m) {
  os << m.x;
  return os;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  ll k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << '\\n';
    return 0;
  }
  ll s = 7;
  for (ll i = 1;; ++i) {
    if (s % k == 0) {
      cout << i << '\\n';
      return 0;
    }
    s += 7 * modpow(10, i, k);
    s %= k;
  }

  end = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<chrono::milliseconds>(end - start).count();
  cerr << elapsed << "ms" << '\\n';
}