Problem p02596 (C++) — buggy submission, verdict: Runtime Error

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//             tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;

ll me(ll x, ll n, ll m) {
  ll res = 1;
  x %= m;
  while (n) {
    if (n & 1)
      res = (1LL * res * x) % m;
    x = (1LL * x * x) % m;
    n >>= 1;
  }
  return res;
}

void solve() {
  ll k;
  lscn(k);

  ll remain = 0;

  for (ll i = 0; i <= (ll)1e6 + 2; ++i) {
    ll here = me(10, i, k);
    here = (here * 7LL) % k;

    remain += here;

    if (remain % k == 0) {
      lprin(i + 1);
      exit(0);
    }

    if (here == 0) {
      prin(-1);
      exit(0);
    }
  }

  prin(-1);
}

int main() {
  int t = 1;
  // scn(t);
  while (t--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//             tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;

ll me(ll x, ll n, ll m) {
  ll res = 1;
  x %= m;
  while (n) {
    if (n & 1)
      res = (1LL * res * x) % m;
    x = (1LL * x * x) % m;
    n >>= 1;
  }
  return res;
}

void solve() {
  ll k;
  lscn(k);

  ll remain = 0;

  for (ll i = 0; i <= (ll)1e6 + 2; ++i) {
    ll here = me(10, i, k);
    here = (here * 7LL) % k;

    remain += here;

    if (remain % k == 0) {
      lprin(i + 1);
      exit(0);
    }

    if (here == 0) {
      prin(-1);
      exit(0);
    }
  }

  assert(0);
}

int main() {
  int t = 1;
  // scn(t);
  while (t--) {
    solve();
  }
  return 0;
}