Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// One implementation to rule them all?
#define db double
#define ll long long int
#define ld long double
// #define int           long long int
#define ull unsigned long long
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, n, b) for (int i = n; i >= b; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof a)
#define mkp make_pair
#define pb push_back
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgp(x)                                                                \
  cerr << "[ " << #x << ".ff : " << x.first << ", " << #x                      \
       << ".ss : " << x.second << " ]" << endl
#define dbgs(x)                                                                \
  cerr << "[ ";                                                                \
  for (auto i : x)                                                             \
    cerr << i << " ";                                                          \
  cerr << " ]" << endl;
#define dbgm(x)                                                                \
  cerr << "----------" << endl;                                                \
  for (auto i : x)                                                             \
    cerr << i.fi << ":" << i.se << endl;                                       \
  cerr << "----------";
#define printdb(i) printf("%.12lf\n", i)
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
// Can use tuples in C++17
using namespace std;
typedef map<int, int> mii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<vii> vvi;
typedef pair<int, int> pii;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
const db PI = acos(-1);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
db eps = 1e-5;
ll mod = (1e9 + 7); // 998244353
void print(vector<int> &arr) {
  for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";

  cout << endl;
}
void printll(vector<ll> &arr) {
  for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";

  cout << endl;
}

ll ncr(ll n, ll r) {
  r = min(r, n - r);
  ll A[r], i, j, B[r];
  iota(A, A + r, n - r + 1);
  iota(B, B + r, 1);

  ll g;
  for (i = 0; i < r; i++)
    for (j = 0; j < r; j++) {
      if (B[i] == 1)
        break;
      g = __gcd(B[i], A[j]);
      A[j] /= g;
      B[i] /= g;
    }
  ll ans = 1;
  for (i = 0; i < r; i++)
    ans = (ans * A[i] % mod);
  return ans;
}
ll binpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

const int SIZE = 200001; // constant SIZE here
bool judge(int a[], int n) {
  static int used[SIZE + 1];
  for (int i = 1; i <= n; i++)
    used[i] = 0;
  for (int i = 0; i < n; i++)
    used[a[i]] = 1;
  for (int i = 1; i <= n; i++) {
    if (!used[i])
      return 0;
  }
  return 1;
}
int n, m, r, u, v, idx = 1, k = 0;
vvi adj(SIZE);
vii color(SIZE, -1), indeg(SIZE, 0), outdeg(SIZE, 0), level(SIZE, 0);
vector<bool> vis(SIZE, false);
stack<int> st;
bool dfs(int u) {
  color[u] = 1;
  bool ans = false;
  for (auto i : adj[u]) {
    if (color[i] == 1)
      ans = true;
    if (color[i] == 0 && dfs(i))
      ans = true;
  }

  color[u] = 2;
  return ans;
}
vector<set<int>> vv(SIZE);
void dfs2(int u, int par) {
  vis[u] = 1;
  //	dbg(u);
  //	dbg(par);
  if (color[u] == -1) {
    set<int> s;
    for (auto i : adj[u])
      s.insert(color[i]);
    for (auto i : adj[par])
      s.insert(color[i]);
    idx = 0;
    //		dbgs(s);
    while (s.find(idx) != s.end()) {
      idx = (idx + 1) % k;
    }
    //		dbg(idx);
    color[u] = idx;
  }
  for (auto i : adj[u]) {
    if (!vis[i])
      dfs2(i, u);
  }
}

int main() {
  // CONDITION && cout << "YES" || cout << "NO"; cout << '\n';
  //////////////////////////////////////////////////////////////////
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
  // For fast IO
  /////////////////////////////////////////////////////////////////////
  // Have you thought about it enough?
  /////////////////////////////////////////////////////////////////////
  //  std::cout.unsetf ( std::ios::floatfield );  // floatfield not set
  //  std::cout.precision(16);
  //  freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
  ll n;
  cin >> n;
  ll ans = 1, num = 7;
  while (num % n != 0) {
    if (ans >= LLONG_MAX) {
      cout << -1 << endl;
      exit(0);
    }
    ans++;
    num = (1LL * num * 10 + 7LL) % n;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// One implementation to rule them all?
#define db double
#define ll long long int
#define ld long double
// #define int           long long int
#define ull unsigned long long
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, n, b) for (int i = n; i >= b; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof a)
#define mkp make_pair
#define pb push_back
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgp(x)                                                                \
  cerr << "[ " << #x << ".ff : " << x.first << ", " << #x                      \
       << ".ss : " << x.second << " ]" << endl
#define dbgs(x)                                                                \
  cerr << "[ ";                                                                \
  for (auto i : x)                                                             \
    cerr << i << " ";                                                          \
  cerr << " ]" << endl;
#define dbgm(x)                                                                \
  cerr << "----------" << endl;                                                \
  for (auto i : x)                                                             \
    cerr << i.fi << ":" << i.se << endl;                                       \
  cerr << "----------";
#define printdb(i) printf("%.12lf\n", i)
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
// Can use tuples in C++17
using namespace std;
typedef map<int, int> mii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<vii> vvi;
typedef pair<int, int> pii;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
const db PI = acos(-1);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
db eps = 1e-5;
ll mod = (1e9 + 7); // 998244353
void print(vector<int> &arr) {
  for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";

  cout << endl;
}
void printll(vector<ll> &arr) {
  for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";

  cout << endl;
}

ll ncr(ll n, ll r) {
  r = min(r, n - r);
  ll A[r], i, j, B[r];
  iota(A, A + r, n - r + 1);
  iota(B, B + r, 1);

  ll g;
  for (i = 0; i < r; i++)
    for (j = 0; j < r; j++) {
      if (B[i] == 1)
        break;
      g = __gcd(B[i], A[j]);
      A[j] /= g;
      B[i] /= g;
    }
  ll ans = 1;
  for (i = 0; i < r; i++)
    ans = (ans * A[i] % mod);
  return ans;
}
ll binpow(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

const int SIZE = 200001; // constant SIZE here
bool judge(int a[], int n) {
  static int used[SIZE + 1];
  for (int i = 1; i <= n; i++)
    used[i] = 0;
  for (int i = 0; i < n; i++)
    used[a[i]] = 1;
  for (int i = 1; i <= n; i++) {
    if (!used[i])
      return 0;
  }
  return 1;
}
int n, m, r, u, v, idx = 1, k = 0;
vvi adj(SIZE);
vii color(SIZE, -1), indeg(SIZE, 0), outdeg(SIZE, 0), level(SIZE, 0);
vector<bool> vis(SIZE, false);
stack<int> st;
bool dfs(int u) {
  color[u] = 1;
  bool ans = false;
  for (auto i : adj[u]) {
    if (color[i] == 1)
      ans = true;
    if (color[i] == 0 && dfs(i))
      ans = true;
  }

  color[u] = 2;
  return ans;
}
vector<set<int>> vv(SIZE);
void dfs2(int u, int par) {
  vis[u] = 1;
  //	dbg(u);
  //	dbg(par);
  if (color[u] == -1) {
    set<int> s;
    for (auto i : adj[u])
      s.insert(color[i]);
    for (auto i : adj[par])
      s.insert(color[i]);
    idx = 0;
    //		dbgs(s);
    while (s.find(idx) != s.end()) {
      idx = (idx + 1) % k;
    }
    //		dbg(idx);
    color[u] = idx;
  }
  for (auto i : adj[u]) {
    if (!vis[i])
      dfs2(i, u);
  }
}

int main() {
  // CONDITION && cout << "YES" || cout << "NO"; cout << '\n';
  //////////////////////////////////////////////////////////////////
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
  // For fast IO
  /////////////////////////////////////////////////////////////////////
  // Have you thought about it enough?
  /////////////////////////////////////////////////////////////////////
  //  std::cout.unsetf ( std::ios::floatfield );  // floatfield not set
  //  std::cout.precision(16);
  //  freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
  ll n;
  cin >> n;
  ll ans = 1, num = 7;
  while (num % n != 0) {
    if (ans >= 100000000LL) {
      cout << -1 << endl;
      exit(0);
    }
    ans++;
    num = (1LL * num * 10 + 7LL) % n;
  }
  cout << ans << endl;
  return 0;
}