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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int k;
bool h[N];
int main() {
  scanf("%d", &k);

  int ans = 1;
  int r = 7 % k;
  if (!r) {
    puts("1");
    return 0;
  }
  while (1) {
    ans++;
    r = (r * 10 + 7) % k;
    if (!r)
      break;

    if (h[r]) {
      ans = -1;
      break;
    }
    h[r] = 1;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}
const int N = 5e5 + 5;
const int M = 1e6 + 5;
int cnt[M];
int a[N];
int ans[N];
int res;
struct info {
  int l, r, id, pos;
} q[N];
bool comp(info x, info y) {
  if (x.pos != y.pos)
    return x.pos < y.pos;
  return x.r < y.r;
}
inline void add(int pla) {
  if (++cnt[a[pla]] == 1)
    res++;
}
inline void del(int pla) {
  if (--cnt[a[pla]] == 0)
    res--;
}
int main() {
  int n, m;
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  int block = sqrt(n);

  read(m);
  int l, r;
  for (int i = 1; i <= m; i++) {
    read(l);
    read(r);
    q[i] = (info){l, r, i, l / block};
  }
  sort(q + 1, q + m + 1, comp);

  l = 1, r = 0;
  for (int i = 1; i <= m; i++) {
    while (l < q[i].l)
      del(l++);
    while (l > q[i].l)
      add(--l);
    while (r > q[i].r)
      del(r--);
    while (r < q[i].r)
      add(++r);
    ans[q[i].id] = res;
  }

  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 1e6 + 100;
ll t, ans = 0;
ll cnt[maxn];
int main() {
  //	freopen("in.txt","r",stdin);
  scanf("%lld", &t);
  cnt[1] = 7 % t;
  for (int i = 2; i <= 1000001; ++i) {
    cnt[i] = (10 * cnt[i - 1]) % t;
  }

  ll tt = 0;
  for (int i = 1; i <= 1000000; ++i) {
    tt += cnt[i];
    if (tt % t == 0) {
      ans = i;
      break;
    } else
      tt %= t;
  }

  if (ans == 0)
    printf("-1");
  else
    printf("%d", ans);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 1e6 + 100;
int n;
ll t, ans;
ll cnt[maxn];
int main() {
  //	freopen("in.txt","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  int a = 0;
  cnt[1] = 7 % t;
  rep(i, 2, 1000100) { cnt[i] = (10 * cnt[i - 1]) % t; }

  ll tt = 0;
  rep(i, 1, 1000100) {
    tt += cnt[i];
    if (tt % t == 0) {
      ans = i;
      break;
    } else
      tt %= t;
  }

  if (ans == 0)
    cout << "-1";
  else
    cout << ans;
  //    system("pause");

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using st = string;
using P = pair<int, int>;
using bl = bool;
using ch = char;
#define mrep(i, m, n) for (int i = m; i < (n); i++)
#define rep(i, n) mrep(i, 0, n)
#define chmax(x, y)                                                            \\
  { x = max(x, y); }
#define chmin(x, y)                                                            \\
  { x = min(x, y); }
#define INF 1e7

vector<int> a(INF);

int main() {
  int k;
  cin >> k;
  a.at(0) = 7 % k;
  mrep(i, 1, k) { a.at(i) = (a.at(i - 1) * 10 + 7) % k; }
  rep(i, k) {
    if (a.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  /*while(true){
      if(k % 2 == 0 || k % 5 == 0){
          ans = -1;
          break;
      }
      if(seven % k == 0){
          break;
      }
      seven = seven * 10 + 7;
      ans++;
  }*/
}

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;
using ll = long long;
using st = string;
using P = pair<int, int>;
using bl = bool;
using ch = char;
#define mrep(i, m, n) for (int i = m; i < (n); i++)
#define rep(i, n) mrep(i, 0, n)
#define chmax(x, y)                                                            \\
  { x = max(x, y); }
#define chmin(x, y)                                                            \\
  { x = min(x, y); }
#define INF 1e10

vector<int> a(INF);

int main() {
  int k;
  cin >> k;
  a.at(0) = 7 % k;
  mrep(i, 1, k) { a.at(i) = (a.at(i - 1) * 10 + 7) % k; }
  rep(i, k) {
    if (a.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  /*while(true){
      if(k % 2 == 0 || k % 5 == 0){
          ans = -1;
          break;
      }
      if(seven % k == 0){
          break;
      }
      seven = seven * 10 + 7;
      ans++;
  }*/
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

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

  if (K % 7 == 0) {
    K /= 7;
  }
  ll L = 9 * K;
  if (L % 2 == 0 || L % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }

  ll am = 1;
  ll ans;
  bool a = false;
  for (ll i = 1; i < 1000000000; i++) {
    am *= 10;
    if (am % L == 1) {
      a = true;
      ans = i;
      break;
    }
    am %= L;
  }

  if (a) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

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

  if (K % 7 == 0) {
    K /= 7;
  }
  ll L = 9 * K;

  ll am = 1;
  ll ans;
  bool a = false;
  for (ll i = 1; i < 1000000000; i++) {
    am *= 10;
    if (am % L == 1) {
      a = true;
      ans = i;
      break;
    }
    am %= L;
  }

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

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

int main() {

  long long k;
  cin >> k;
  int t = 7;
  int cnt = 1;
  if (k % 2 == 0 || k % 5 == 0)
    cout << "-1" << endl;
  else {
    while (t % k != 0) {
      t = (t * 10 + 7) % k;
      cnt++;
    }

    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {

  long long k;
  cin >> k;
  long long t = 7;
  int cnt = 1;
  if (k % 2 == 0)
    cout << "-1" << endl;
  else {
    while (t % k != 0) {
      t = (t * 10 + 7) % k;
      cnt++;
    }

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

#include <iostream>
#include <unordered_set>
using namespace std;
typedef long long int ll;
int main() {

  unordered_set<int> fn;
  ll c = 1;

  ll x;
  cin >> x;
  int temp = 7;
  fn.insert(7);
  if (x % 2 == 0) {

    cout << -1;
    return 0;
  }

  while ((temp % x) != 0) {

    ll r = temp % x;
    temp = r * 10 + 7;

    c++;
    if (fn.find(temp) != fn.end()) {
      cout << -1;
      return 0;
    }
    fn.insert(temp);
  }
  cout << c;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long int ll;
int main() {
  ll c = 1;

  ll x;
  cin >> x;
  int temp = 7;
  if (x % 2 == 0) {

    cout << -1;
    return 0;
  }

  while ((temp % x) != 0) {
    ll r = temp % x;
    temp = r * 10 + 7;

    c++;
  }
  cout << c;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  vector<int> dp(10000000);
  long long int ans = 1;
  long long int mod = 0;
  while (true) {
    long long int mod = ((mod * 10) + 7) % k;
    if (mod == 0) {
      cout << ans;
      break;
    } else if (dp[mod]) {
      cout << "-1";
      break;
    }
    dp[mod] = 1;

    ans++;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  vector<int> dp(1000000);
  long long int ans = 1;
  long long int temp = 7;
  while (true) {
    long long int mod = temp % k;
    if (mod == 0) {
      cout << ans;
      break;
    } else if (dp[mod]) {
      cout << "-1";
      break;
    }
    dp[mod] = 1;
    temp = (temp * 10) + 7;
    ans++;
  }
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  int k, m;
  cin >> k;
  int i = 0, j = 0;
  vector<int> mm(10000001), kk(k, 0);

  mm.at(i) = 7 % k;
  // cout << mm.at(i) << endl;

  rep(i, 10000001) {
    // cout << mm.at(i) << endl;
    if (mm.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
    kk.at(mm.at(i)) += 1;
    if (kk.at(mm.at(i)) == 2) {
      cout << -1 << endl;
      return 0;
    }

    mm.at(i + 1) = ((mm.at(i) * 10 + 7) % k);
  }
  /*	rep(j, k-1) {

                  if (kk.at(mm.at(j)) == 2) {
                          cout << -1 <<endl;
                          return 0;
                  }
          }*/
  return 0;
} // どうして・・・(´･_･`)

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  int k, m;
  cin >> k;
  int i = 0, j = 0;
  vector<int> mm(10000001), kk(k, 0);

  mm.at(i) = 7 % k;
  // cout << mm.at(i) << endl;

  rep(i, 10000001) {
    // cout << mm.at(i) << endl;
    if (mm.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
    kk.at(mm.at(i)) += 1;

    mm.at(i + 1) = ((mm.at(i) * 10 + 7) % k);
  }
  rep(j, k - 1) {

    if (kk.at(mm.at(j)) == 2) {
      cout << -1 << endl;
      return 0;
    }
  }
  return 0;
} // どうして・・・(´･_･`)
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define lln long long int
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

int freq[1000001];

int main() {
  IOS;
  int k;
  cin >> k;
  if (!(k % 2)) {
    cout << -1 << endl;
    return 0;
  }
  int a = 7;
  for (int i = 0;; i++) {
    if (a % k == 0) {
      cout << i + 1 << endl;
      return 0;
    } else {
      int b = a % k;
      if (freq[b])
        break;
      freq[b]++;
      a = (a * 10 + 7) % k;
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define lln long long int
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1";
    return 0;
  }
  if (k % 5 == 0) {
    cout << "-1";
    return 0;
  }
  int p = 0, c = 0;
  while (1 == 1) {
    c++;
    p = (p * 10 + 7) % k;
    if (p == 0) {
      break;
    }
  }
  cout << c;
  return 0;
}

Error: TLE

Fixed submission:

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

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

long long mod_pow(long long x, long long n, long long &p) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

int main() {
  long long k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0)
    cout << -1 << endl;
  else {
    long long ans = 1;
    long long k9 = k * 9;
    while (true) {
      long long num = (7 * (mod_pow(10, ans, k9) - 1)) / 9;
      if (num % k == 0)
        break;
      else
        ans++;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

long long mod_pow(long long x, long long n, long long &p) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

int main() {
  long long k;
  cin >> k;
  bool j = false;
  if (k % 2 == 0 || k % 5 == 0)
    cout << -1 << endl;
  long long ans = 1;
  long long k9 = k * 9;
  while (true) {
    long long num = (7 * (mod_pow(10, ans, k9) - 1)) / 9;
    if (num % k == 0)
      break;
    else
      ans++;
  }
  cout << ans << endl;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int k;
  cin >> k;
  int ans = 1, m = 7;

  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (k == 1 || k == 7) {
    cout << 1 << endl;
    return 0;
  }
  while (m != 0) {
    ans++;
    m = (10 * m + 7) % k;
  }

  cout << ans << endl;

  return 0;
}
/*
 */


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() {
  long long k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  } else if (k == 1 || k == 7) {
    cout << 1 << endl;
    return 0;
  }
  int seven = 0;
  // unsigned long long i;
  while (true) {
    // string s = "7";
    // cnt++;{
    seven = seven * 10 + 7;
    if (seven % k == 0) {
      cout << int(log10(seven)) + 1 << endl;
      return 0;
    } else if (int(log10(seven)) > 100000000000) {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int K;
  cin >> K;
  int cnt = 0, now = 0;
  auto start = chrono::system_clock::now();
  while (++cnt) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\\n", 0;
    if (static_cast<double>(chrono::duration_cast<chrono::microseconds>(
                                chrono::system_clock::now() - start)
                                .count() /
                            1000.0) > 1990)
      return cout << -1 << "\\n", 0;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int K;
  cin >> K;
  int cnt = 0, now = 0;
  auto start = chrono::system_clock::now();
  while (++cnt) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\\n", 0;
    if (static_cast<double>(chrono::duration_cast<chrono::microseconds>(
                                chrono::system_clock::now() - start)
                                .count() /
                            1000.0) > 1999)
      return cout << -1 << "\\n", 0;
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int K;
  cin >> K;
  auto start = system_clock::now();
  for (int cnt = 1, now = 0;
       static_cast<double>(
           duration_cast<milliseconds>(system_clock::now() - start).count()) <
       1990;
       cnt++) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\\n", 0;
  }
  cout << -1 << "\\n";
}

Error: TLE

Fixed submission:

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

int main() {
  int K;
  cin >> K;
  auto start = system_clock::now();
  for (int cnt = 1, now = 0;
       static_cast<double>(
           duration_cast<milliseconds>(system_clock::now() - start).count()) <
       1999;
       cnt++) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\\n", 0;
  }
  cout << -1 << "\\n";
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  if (n % 2 == 0 || n % 5 == 0) {
    cout << -1;
    return 0;
  }
  int here = 7 % n, cnt = 1;
  while (here) {
    here *= 10;
    here += 7;
    here %= n;
    cnt++;
  }
  cout << cnt;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll n, k, cnt;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  k = 7;
  cnt = 1;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << "-1";
    return 0;
  }
  k %= n;
  while (k != 0) {
    k *= 10;
    k += 7;
    k %= n;
    cnt++;
  }
  cout << cnt;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

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

  ll m = 0;
  ll a = 1;
  int i = 1;

  while (i <= k) {
    m += (7 * a);
    m %= k;
    if (m == 0) {
      cout << i << endl;
      return 0;
    }
    a *= 10;
    a %= k;
    i++;
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

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

  ll m = 0;
  ll a = 1;
  int i = 1;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  while (1) {
    m += (7 * a);
    m %= k;
    if (m == 0) {
      cout << i << endl;
      break;
    }
    a *= 10;
    a %= k;
    i++;
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  string s;
  cin >> s;
  int K = stoi(s);
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int count = s.length();

  int seven = 7;
  for (int j = 1; j < count; ++j) {
    seven = seven * 10 + 7;
  }
  if (seven < K) {
    seven = seven * 10 + 7;
    ++count;
  }

  long long preAmari = seven % K;
  for (int i = 0;; ++i) {
    if (preAmari % K == 0) {
      cout << count + i << endl;
      return 0;
    }
    preAmari = (preAmari * 10 + 7) % K;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  string s;
  cin >> s;
  int K = stoi(s);
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int count = s.length();

  int seven = 7;
  for (int j = 1; j < count; ++j) {
    seven = seven * 10 + 7;
  }
  if (seven < K) {
    seven = seven * 10 + 7;
    ++count;
  }

  long long preAmari = seven % K;
  for (int i = 0;; ++i) {
    if (preAmari % K == 0) {
      cout << count + i << endl;
      return 0;
    }
    preAmari = (preAmari * 10 + 7) % K;
  }

  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
ll rsept(int k) {
  ll value = 7;
  if (k % 2 == 0 || k % 5 == 0)
    return -1;
  for (ll i = 1; i < 1e9; i++) {
    if (value % k == 0) {
      return i;
    } else
      value = (value * 10 % k + 7 % k);
  }
  return -1;
}
int main() {
  ll k;
  cin >> k;
  cout << rsept(k) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long sept(long long k, long long a, long long *count) {

  if (k % 2 == 0 || k % 5 == 0)
    return -1;
  else if (a % k == 0)
    return *count;
  else {
    (*count)++;
    return sept(k, a * 10 + 7, count);
  }
}
int main() {
  long long count = 1;
  long long k, a = 7;
  cin >> k;
  cout << sept(k, a, &count) << endl;
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int64_t MOD = 1000000007;
double PI = 3.141592653589793;

int main() {
  int K;
  cin >> K;
  if (K % 2 == 0 || K % 5 == 0)
    cout << -1;
  else {
    int tmp = 7, ans = 1;
    while (true) {
      if (tmp % K == 0) {
        cout << ans;
        break;
      } else {
        ans++;
        tmp = (tmp * 10 + 7) % K;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

int64_t MOD = 1000000007;
double PI = 3.141592653589793;

int main() {
  int K;
  cin >> K;
  if (K % 2 == 0 || K % 2 == 5)
    cout << -1;
  else {
    int tmp = 7, ans = 1;
    while (true) {
      if (tmp % K == 0) {
        cout << ans;
        break;
      } else {
        ans++;
        tmp = (tmp * 10 + 7) % K;
      }
    }
  }
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long int K;
  cin >> K;
  long long int N = 7;
  long long int i = 1;
  long long int output = -1;

  if (K % 2 == 0) {
    cout << output << '\\n';
    return 0;
  }

  while (i < 7 * K + 1) {
    if (N % K == 0) {
      output = i;
      break;
    }
    N %= K;
    N = 10 * N + 7;

    i += 1;
  }
  cout << output << '\\n';
}

Error: TLE

Fixed submission:

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

int main() {
  long long int K;
  cin >> K;
  long long int N = 7;
  long long int i = 1;
  long long int output = -1;

  if (K % 2 == 0) {
    cout << output << '\\n';
    return 0;
  }

  while (i < LLONG_MAX) {
    if (N % K == 0) {
      output = i;
      break;
    }
    N %= K;
    N = 10 * N + 7;

    i += 1;
  }
  cout << output << '\\n';
}