alexjercan/bugnet huggingface dataset:
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll k;
  cin >> k;

  if (k % 2 == 0) {
    cout << -1 << '\\n';
    return 0;
  }

  ll ten = 1;
  ll tmp = 0;
  int cnt = 1;
  for (int i = 0; i < 1e7; i++) {
    ll add = (7 * ten) % k;
    tmp = (tmp + add) % k;
    ten = (ten * 10) % k;
    if (tmp == 0) {
      cout << cnt << '\\n';
      return 0;
    }
    cnt++;
  }
  cout << -1 << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll k;
  cin >> k;

  if (k % 2 == 0) {
    cout << -1 << '\\n';
    return 0;
  }

  ll ten = 1;
  ll tmp = 0;
  int cnt = 1;
  while (true) {
    ll add = (7 * ten) % k;
    tmp = (tmp + add) % k;
    ten = (ten * 10) % k;
    if (tmp == 0) {
      cout << cnt << '\\n';
      return 0;
    }
    cnt++;
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
using namespace std;

long mod(long x, long p) {
  long res = x % p;
  if (res < 0) {
    return res + p;
  } else {
    return res;
  }
}

int main() {
  long k = LONG_MIN;
  cin >> k;
  int i = 1;
  long x = 7;
  while (i < 10'000'000) {
    if (mod(x, k) == 0) {
      cout << i << "\\n";
      return 0;
    }
    x = mod(10 * x + 7, k);
    ++i;
  }
  cout << -1 << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
using namespace std;

long mod(long x, long p) {
  long res = x % p;
  if (res < 0) {
    return res + p;
  } else {
    return res;
  }
}

int main() {
  long k = LONG_MIN;
  cin >> k;
  int i = 1;
  long x = 7;
  while (i < 1000'000'000) {
    if (mod(x, k) == 0) {
      break;
    }
    x = mod(10 * x + 7, k);
    ++i;
  }
  cout << i << "\\n";
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 1e6 + 10;
bool mp[M];
int main() {
  int k;
  cin >> k;
  // if(!(k&1))
  //     return puts("-1"),0;
  int ans = 1, re = 7 % k;
  mp[re] = 1;
  while (re != 0) {
    re = (re * 10 + 7) % k, ans++;
    if (mp[re])
      return puts("-1"), 0;
    mp[re] = 1;
  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 1e6 + 10;
bool mp[M];
int main() {
  int k;
  cin >> k;
  int ans = 1, re = 7 % k;
  mp[re] = 1;
  while (re % k != 0) {
    re = (re * 10 % k + 7) % k, ans++;
    if (mp[re])
      return puts("-1"), 0;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define mp make_pair
#define PI acos(-1)
using namespace std;
bool ar[1000005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll i, j, k, x, y, z, m, t, n, b, a, c, d, l, r;
  cin >> n;
  x = 0;
  ll cnt = 1;
  while (1) {
    x *= 10;
    x += 7;
    x %= n;
    if (ar[x])
      break;
    if (x == 0) {
      cout << cnt << '\\n';
      return 0;
    }
    ar[x] = 1;
    cnt++;
  }
  cout << -1 << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define mp make_pair
#define PI acos(-1)
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll i, j, k, x, y, z, m, t, n, b, a, c, d, l, r;
  cin >> n;
  if (!(n & 1))
    cout << -1 << '\\n';
  else {
    x = 7;
    ll cnt = 1;
    while (1) {
      if (x % n == 0)
        break;
      cnt++;
      x *= 10;
      x += 7;
    }
    cout << cnt << '\\n';
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<bhaai.h>
#include <bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a, e) for (int i = a; i <= e; i++)
#define yah_faster_baby                                                        \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

to ans, n, k, m, r[100005], p[100005], a, b;
// vector <int> v[100005];
// char s[100005];
double d, x;
int main() {
  yah_faster_baby cin >> n;
  if (n % 2 == 0) {
    cout << "-1\\n";
    return 0;
  }
  ans = 1;
  k = 7;
  while (ans <= 10000000 && k % n) {
    ++ans;
    k *= 10;
    k += 7;
    k %= n;
  }
  if (ans == 10000001)
    cout << "-1\\n";
  else
    cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// #include<bhaai.h>
#include <bits/stdc++.h>
using namespace std;
#define to int64_t
#define mo 1000000007
#define fr(a, e) for (int i = a; i <= e; i++)
#define yah_faster_baby                                                        \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

to ans, n, k, m, r[100005], p[100005], a, b;
// vector <int> v[100005];
// char s[100005];
double d, x;
int main() {
  yah_faster_baby cin >> n;
  if (n % 2 == 0) {
    cout << "-1\\n";
    return 0;
  }
  ans = 1;
  k = 7;
  while (k % n) {
    ++ans;
    k *= 10;
    k += 7;
    k %= n;
  }
  cout << ans << endl;
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll num = 0;
  ll temp = 7;

  for (int i = 1; i <= 10000000; i++) {
    num = (num % n + temp % n) % n;
    if (num == 0) {
      cout << i;
      return 0;
    }
    temp = (temp * 10) % n;
  }
  cout << -1;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 1000000007LL
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  stack<char> ans;
  ll count = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W' && ans.size() == 0)
      ans.push('W');
    else if (s[i] == 'R' && ans.size() != 0) {
      ans.pop();
      count++;
    }
  }
  ll pos = 0;
  while (pos < n) {
    ll find = s.find("WWRR", pos);
    if (find < n) {
      count++;
      pos = find + 1;
    } else {
      break;
    }
  }
  cout << count;

  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int k;
  cin >> k;
  bool flag = false;

  ll l = (k % 7 == 0) ? 9 * k / 7 : 9 * k;
  ll n = 1;
  for (ll i = 1; i <= l; i++) {
    n = (n % l) * 10 % l;
    if (n == 1) {
      cout << i << endl;
      flag = true;
      break;
    }
  }

  if (!flag)
    cout << -1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int k;
  cin >> k;
  bool flag = false;

  ll l = (k % 7 == 0) ? 9 * k / l : 9 * k;
  ll n = 1;
  for (ll i = 1; i <= l; i++) {
    n = (n % l) * 10 % l;
    if (n == 1) {
      cout << i << endl;
      flag = true;
      break;
    }
  }

  if (!flag)
    cout << -1 << endl;

  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
using namespace std;

const int A = 1e6 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

int k, a[A];

signed main() {
  k = read();
  for (int i = 1; i <= k; i++) {
    a[i] = a[i - 1] * 10 + 7;
    a[i] %= k;
    if (a[i] == 0) {
      cout << i << '\\n';
      return 0;
    }
  }
  cout << -1 << '\\n';
}

Error: 0

Fixed submission:

/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
using namespace std;

const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

int k, a[A];

signed main() {
  k = read();
  for (int i = 1; i <= k; i++) {
    a[i] = a[i - 1] * 10 + 7;
    a[i] %= k;
    if (a[i] == 0) {
      cout << i << '\\n';
      return 0;
    }
  }
  cout << -1 << '\\n';
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int k;
    cin >> k;
    if (k % 2 == 0) {
      cout << "-1\\n";
      return 0;
    }
    ll ctr = 0;
    ll val = 7;
    ll val1 = 0;
    set<pair<int, int>> s;
    do {
      val = val % k;
      val1 = (val1 + val) % k;
      if (s.find(make_pair(val, val1)) != s.end()) {
        ctr = -1;
        break;
      } else
        s.insert(make_pair(val, val1));
      ctr++;
      val *= 10;
    } while ((val1 % k) != 0);
    cout << ctr << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    int k;
    cin >> k;
    if (k % 2 == 0) {
      cout << "-1\\n";
      return 0;
    }
    ll ctr = 0;
    ll val = 7;
    ll val1 = 0;
    do {
      val = val % k;
      val1 = (val1 + val) % k;
      ctr++;
      val *= 10;
    } while ((val1 % k) != 0);
    cout << ctr << "\\n";
  }
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

// 二分探索法
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF ll(1e+18) + 100;

int main() {
  set<int> s;
  int k;
  cin >> k;
  int x = 7 % k;
  ll b = 7;
  ll i = 1;
  while (x % k != 0) {
    //        int pre=x;
    s.insert(x);
    b *= 10;
    x += b % k;
    x %= k;
    if (s.count(x)) {
      cout << "-1" << endl;
      return 0;
    }
    b %= k;
    i++;
  }
  /*
  rep(i,20){
              b*=10;
      x+=b%k;
      x%=k;
      b%=k;
      cout<<x%k<<endl;
      i++;
  }
  */
  cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

// 二分探索法
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF ll(1e+18) + 100;

int main() {
  int k;
  cin >> k;
  int x = 7 % k;
  ll b = 7;
  ll i = 1;
  while (x % k != 0) {
    int pre = x;
    b *= 10;
    x += b % k;
    x %= k;
    if (pre == x) {
      cout << "-1" << endl;
      return 0;
    }
    b %= k;
    i++;
  }
  /*
  rep(i,20){
              b*=10;
      x+=b%k;
      x%=k;
      b%=k;
      cout<<x%k<<endl;
      i++;
  }
  */
  cout << i << endl;
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> v = vector<int>(k, -1);
  v[0] = 0;
  ll a = 0;
  ll idx = 1;
  while (true) {
    a = a * 10 + 7;
    a %= k;
    if (v[a] != -1) {
      cout << idx - v[a] << endl;
      return 0;
    } else {
      v[a] = idx;
    }
    idx++;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll k;
  vector<int> v = vector<int>(k, -1);
  v[0] = 0;
  ll a = 0;
  ll idx = 1;
  while (true) {
    a = a * 10 + 7;
    a %= k;
    if (v[a] != -1) {
      cout << idx - v[a] << endl;
      return 0;
    } else {
      v[a] = idx;
    }
    idx++;
  }
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\\n"
#define ___                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

const int N = (int)1e5;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ___

      ll int n = 0,
             k, x = 0;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1;
    return 0;
  }
  while (1) {
    n = ((n * 10) + 7) % k;
    x++;
    if (n == 0)
      break;
    else if (x == (int)1e8 + (int)1e7) {
      cout << -1;
      return 0;
    }
  }
  cout << x;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define L long
#define endl "\\n"
#define ___                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

const int N = (int)1e5;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ___

      ll int n = 0,
             k, x = 0;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1;
    return 0;
  }
  while (1) {
    n = ((n * 10) + 7) % k;
    x++;
    if (n == 0)
      break;
    else if (x == (int)2e8) {
      cout << -1;
      return 0;
    }
  }
  cout << x;

  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;

  long long x = 0;
  for (int i = 1; i < 2000000; i++) {
    x = (x * 10 + 7) % k;
    if (x == 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long K;
  cin >> K;

  if (K % 2 == 0 || K < 7) {
    cout << -1 << endl;
  } else {
    string seven = "";
    for (int i = 0; i < 1000000000; i++) {
      seven.append("7");

      unsigned long long s = stoll(seven);

      cout << s << endl;

      if (s % K == 0) {
        cout << i + 1 << endl;
        break;
      }
    }

    cout << -1 << endl;
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  long long K;
  cin >> K;

  if (K % 2 == 0)
    cout << -1 << endl;
  else {
    long long ans = 1;
    long long mod = 7 % K;

    while (mod != 0) {
      ans++;
      mod = (mod * 10 + 7) % K;
      if (ans > 1000000) {
        cout << -1 << endl;
        return 0;
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  long long K;
  cin >> K;

  if (K % 2 == 0)
    cout << -1 << endl;
  else {
    long long ans = 1;
    long long mod = 7 % K;

    while (mod != 0) {
      ans++;
      mod = (mod * 10 + 7) % K;
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
  int k, i, ans = -1, tmp = 0;
  cin >> k;
  for (i = 1; i <= k + 100; i++) {
    tmp = tmp * 10 + 7;
    tmp %= k;
    if (!tmp) {
      ans = i;
      break;
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
  int k, i, ans = -1, tmp = 0;
  for (i = 1; i <= k + 100; i++) {
    tmp = tmp * 10 + 7;
    tmp %= k;
    if (!tmp) {
      ans = i;
      break;
    }
  }
  cout << ans;
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k, num = 0, cnt = 0;
int main() {
  cin >> k;
  while (cnt <= k) {
    num = (num * 10 + 7) % k;
    cnt++;
    if (!num) {
      cout << cnt;
      return 0;
    }
  }
  cout << "-1";
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k, num = 0, cnt = 0;
int main() {
  while (cnt <= k) {
    num = (num * 10 + 7) % k;
    cnt++;
    if (!num) {
      cout << cnt;
      return 0;
    }
  }
  cout << "-1";
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 1;
  int cur = 7 % k;
  while (cur != 0) {
    cur = 10 * cur + 7;
    cur %= k;
    ++ans;
    if (ans > 100000000) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 1;
  int cur = 7 % k;
  while (cur != 0) {
    cur = 10 * cur + 7;
    cur %= k;
    ++ans;
  }
  cout << ans << endl;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool ch1000000(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MAX = 42;

int main() {
  ll K;
  cin >> K;

  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<ll> res(K, 0);
  ll cnt = 1;
  ll a = 7;
  while (true) {
    if (a == 0) {
      break;
    } else {
      while (a < K) {
        a *= 10;
        a += 7;
        cnt++;
      }
      a %= K;
      if (res[a] == 1) {
        cout << -1 << endl;
        return 0;
      }
      res[a] = 1;
    }
  }

  cout << cnt << endl;

  // cout << setprecision(8) << ans << endl;  // For double
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool ch1000000(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MAX = 42;

int main() {

  ll K;
  cin >> K;

  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  ll digit = floor(log10(K) + 1);
  ll times = ll(pow(10, digit));
  ll add = 7;
  rep(i, 0, digit - 1) {
    add *= 10;
    add += 7;
  }
  ll cnt = 1;
  ll a = 7;
  while (true) {
    if (a == 0) {
      break;
    } else {
      while (a < K) {
        a *= 10;
        a += 7;
        cnt++;
      }
      a %= K;
    }
  }

  cout << cnt << endl;

  // cout << setprecision(8) << ans << endl;  // For double
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

int main() {
  ll k;
  cin >> k;

  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  if (k % 7 == 0) {
    k = k / 7;
  }

  ll val = 1;
  for (ll times = 1; times <= 2000000; times++) {
    if (val % k == 0) {

      cout << fixed << times << endl;
      return 0;
    }
    val = (val % k) * 10 + 1;
  }

  cout << fixed << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

int main() {
  ll k;
  cin >> k;

  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  if (k % 7 == 0) {
    k = k / 7;
  }

  ll val = 1;
  ll times = 1;
  while (true) {
    if (val % k == 0) {

      cout << fixed << times << endl;
      return 0;
    }
    val = (val % k) * 10 + 1;
    times++;
  }

  return 0;
}