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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

void solve() {
  int k;
  cin >> k;
  vector<int> rem;
  int mk[k];
  memset(mk, 0, sizeof(mk));
  mk[7 % k] = 1;
  int cur = 7;
  int ans = 1;
  while (1) {
    if (cur % k == 0) {
      cout << ans;
      return;
    }
    ans++;
    cur = (cur * 10 + 7) % k;
    if (mk[cur]) {
      cout << -1;
      return;
    }
    mk[cur] = 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

void solve() {
  int k;
  cin >> k;
  vector<int> rem;
  int st = 7 % k;
  int cur = 7;
  int ans = 1;
  while (1) {
    if (cur % k == 0) {
      cout << ans;
      return;
    }
    ans++;
    cur = (cur * 10 + 7) % k;
    if (cur == st) {
      cout << -1;
      return;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}