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

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
Apparently, the tree supports the same operations as the set
(at least I haven't any problems with them before), but also there are two new
features — find_by_order() — The first returns an iterator to the k-th largest
element (counting from zero) order_of_key() — the number of items in a set that
are strictly smaller than our item. https://codeforces.com/blog/entry/11080
using namespace __gnu_pbds; */
using namespace std;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int test = 1; test <= t; test++)
#define int long long
// #define MOD 1000000007
#define pb push_back
#define ins insert
#define fast()                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ld long double
#define endl '\\n'
int m(int a, int MOD) { return ((a % MOD) + MOD) % MOD; }
int p(int a, int b, int MOD) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = m(m(res, MOD) * m(a, MOD), MOD);
    a = m(m(a, MOD) * m(a, MOD), MOD);
    b >>= 1;
  }
  return res;
}
signed main() {
  fast();
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = 0;
  int num = 0;
  vector<int> pre(1000000);
  for (int i = 0; i < 1000000; i++) {
    pre[i] = m(m(7 * p(10, i, k), k), k);
  }
  while (x < 1000000) {
    num = m(m(m(num, k) + pre[x], k), k);
    // num %= k;
    x++;
    if (num == 0)
      break;
  }
  if (num == 0)
    cout << x << endl;
  else
    cout << -1 << endl;
  cerr << "\\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
Apparently, the tree supports the same operations as the set
(at least I haven't any problems with them before), but also there are two new
features — find_by_order() — The first returns an iterator to the k-th largest
element (counting from zero) order_of_key() — the number of items in a set that
are strictly smaller than our item. https://codeforces.com/blog/entry/11080
using namespace __gnu_pbds; */
using namespace std;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int test = 1; test <= t; test++)
#define int long long
// #define MOD 1000000007
#define pb push_back
#define ins insert
#define fast()                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ld long double
#define endl '\\n'
int m(int a, int MOD) { return ((a % MOD) + MOD) % MOD; }
int p(int a, int b, int MOD) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = m(m(res, MOD) * m(a, MOD), MOD);
    a = m(m(a, MOD) * m(a, MOD), MOD);
    b >>= 1;
  }
  return res;
}
signed main() {
  fast();
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = 0;
  int num = 0;
  while (true) {
    num = m(m(num, k) + m(7 * p(10, x, k), k), k);
    // num %= k;
    x++;
    if (x == 2000000) {
      cout << -1 << endl;
      return 0;
    }
    if (num == 0)
      break;
  }
  cout << x << endl;
  cerr << "\\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\\n";
}