prithivMLmods/Coder-Stat huggingface dataset:
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n, a[110000];
int f[1100000];
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (f[a[i]] == 0 && (i == n || a[i] != a[i + 1]))
      ++cnt;
    for (int j = 1; a[i] * j <= a[n]; ++j)
      f[a[i] * j] = 1;
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n, a[210000];
int f[1100000];
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (f[a[i]] == 0 && (i == n || a[i] != a[i + 1]))
      ++cnt;
    for (int j = 1; a[i] * j <= a[n]; ++j)
      f[a[i] * j] = 1;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 1000001;

int n, cnt[N];
vector<int> a;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  map<int, int> f;
  for (auto &i : a) {
    f[i]++;
    if (f[i] == 3)
      continue;
    for (int j = i; j < N; j += i) {
      cnt[j]++;
    }
  }
  int ans = 0;
  for (auto &i : a) {
    if (cnt[i] == 1)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 1000001;

int n, cnt[N];
vector<int> a;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  map<int, int> f;
  for (auto &i : a) {
    f[i]++;
    if (f[i] >= 3)
      continue;
    for (int j = i; j < N; j += i) {
      cnt[j]++;
    }
  }
  int ans = 0;
  for (auto &i : a) {
    if (cnt[i] == 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n;
  cin >> n;
  vint a(n);
  vector<pair<int, int>> aa;
  bool b[(int)1e6];
  fill(b, b + (int)1e6, true);
  REP(i, n) cin >> a[i];

  sort(a.begin(), a.end());

  int ans = 0;
  REP(i, n) {
    int j = a[i] * 2;
    while (j <= (int)1e6) {
      b[j - 1] = false;
      j += a[i];
    }
    if (i != 0 && a[i] == a[i - 1])
      b[a[i] - 1] = false;
  }

  REP(i, n) {
    if (b[a[i] - 1])
      ans++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define vint vector<int>

int main() {
  int n;
  cin >> n;
  vint a(n);
  vector<pair<int, int>> aa;
  bool b[(int)1e6];
  fill(b, b + (int)1e6, true);
  REP(i, n) cin >> a[i];

  sort(a.begin(), a.end());

  int ans = 0;
  REP(i, n) {
    if (b[a[i] - 1] == false)
      continue;
    int j = a[i] * 2;
    while (j <= (int)1e6) {
      b[j - 1] = false;
      j += a[i];
    }
    if (i != 0 && a[i] == a[i - 1])
      b[a[i] - 1] = false;
  }

  REP(i, n) {
    if (b[a[i] - 1])
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX = 1e6;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &e : a)
    cin >> e;
  sort(a.begin(), a.end());

  int r = 0;
  vector<bool> v(MAX + 1, true);
  for (int i = 0; i < n; i++) {
    if ((i == n || a[i] != a[i + 1]) && v[a[i]])
      r++;
    for (int j = a[i]; j <= (int)MAX; j += a[i])
      v[j] = false;
  }
  cout << r << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX = 1e6;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &e : a)
    cin >> e;
  sort(a.begin(), a.end());

  int r = 0;
  vector<bool> v(MAX + 1, true);
  for (int i = 0; i < n; i++) {
    if (!v[a[i]])
      continue;
    if (i == n || a[i] != a[i + 1])
      r++;
    for (int j = a[i]; j <= (int)MAX; j += a[i])
      v[j] = false;
  }
  cout << r << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define COS 200005

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<bool> b(COS, false);
  ll i, j;
  ll ans = 0;

  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  rep(i, n - 1) {
    if (b[a[i]] == true)
      continue;
    if (a[i] == a[i + 1]) {
      b[a[i]] = true;
      // continue;
    }
    for (j = 2; j < a[n - 1] / a[i] + 1; j++) {
      b[j * a[i]] = true;
    }
  }
  rep(i, n) {
    if (b[a[i]] == false)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

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

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define COS 2000005

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<bool> b(COS, false);
  ll i, j;
  ll ans = 0;

  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  rep(i, n - 1) {
    if (b[a[i]] == true)
      continue;
    if (a[i] == a[i + 1]) {
      b[a[i]] = true;
      // continue;
    }
    for (j = 2; j < a[n - 1] / a[i] + 1; j++) {
      b[j * a[i]] = true;
    }
  }
  rep(i, n) {
    if (b[a[i]] == false)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back

ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
map<ll, ll> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vl v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  ll c = 0;
  for (ll i = 0; i < n; i++) {
    ll d = v[i];
    mp[d]--;
    int f = 0;
    for (ll j = 1; j * j <= d; j++) {
      if (d % j == 0) {
        if (mp[j] > 0 || mp[d / j] > 0) {
          f = 1;
          break;
        }
      }
    }
    mp[d]++;
    if (f == 0)
      c++;
  }
  cout << c << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back

ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
vl mp(1000006, 0);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vl v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  ll c = 0;
  for (ll i = 0; i < n; i++) {
    ll d = v[i];
    mp[d]--;
    int f = 0;
    for (ll j = 1; j * j <= d; j++) {
      if (d % j == 0) {
        if (mp[j] > 0 || mp[d / j] > 0) {
          f = 1;
          break;
        }
      }
    }
    mp[d]++;
    if (f == 0)
      c++;
  }
  cout << c << "\n";
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int n;
  cin >> n;

  int a[200001];

  for (int i = 1; i <= 2000000; i++) {
    a[i] = 0;
  }
  rep(i, n) {
    int x;
    cin >> x;
    a[x]++;
  }

  int ans = 0;

  for (int i = 1; i <= 2000000; i++) {
    if (a[i]) {
      if (a[i] == 1) {
        ans++;
      }
      for (int j = i; j <= 2000000; j += i) {
        a[j] = 0;
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int n;
  cin >> n;

  int a[2000001];

  for (int i = 1; i <= 2000000; i++) {
    a[i] = 0;
  }
  rep(i, n) {
    int x;
    cin >> x;
    a[x]++;
  }

  int ans = 0;

  for (int i = 1; i <= 2000000; i++) {
    if (a[i]) {
      if (a[i] == 1) {
        ans++;
      }
      for (int j = i; j <= 2000000; j += i) {
        a[j] = 0;
      }
    }
  }

  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\n"
#define int ll

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(rng(A));
  vector<bool> b(1000001, true);
  bool ng = true;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == A[i + 1])
      for (int j = A[i]; j <= 1000000; j += A[i])
        b[j] = false;
    if (b[A[i]]) {
      ans++;
      for (int j = A[i]; j <= 1000000; j += A[i])
        b[j] = false;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\n"
#define int ll

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(rng(A));
  vector<bool> b(1000001, true);
  bool ng = true;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (!b[A[i]])
      continue;
    if (A[i] == A[i + 1])
      for (int j = A[i]; j <= 1000000; j += A[i])
        b[j] = false;
    if (b[A[i]]) {
      ans++;
      for (int j = A[i]; j <= 1000000; j += A[i])
        b[j] = false;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <vector>

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

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> t(200'002);
  sort(a.begin(), a.end());

  for (int i : a) {
    t[i]++;
    if (t[i] > 1)
      continue;

    for (int j = i * 2; j < t.size(); j += i) {
      t[j]++;
    }
  }

  int ans = 0;
  for (int i : a) {
    if (t[i] == 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <vector>

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

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> t(1'000'001);
  sort(a.begin(), a.end());

  for (int i : a) {
    t[i]++;
    if (t[i] > 1)
      continue;

    for (int j = i * 2; j < t.size(); j += i) {
      t[j]++;
    }
  }

  int ans = 0;
  for (int i : a) {
    if (t[i] == 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) begin(x), end(x)
#define gets(s) fgets(s, size(s), stdin)
using namespace std;
typedef long long ll;
typedef string str;
template <typename T> using vec = vector<T>;
template <typename T> using uset = unordered_set<T>;
template <typename T, typename S> using umap = unordered_map<T, S>;
const size_t mbs = sizeof(bitset<1>) * 8;
template <size_t N> using bit = bitset<mbs *((N + mbs - 1) / mbs)>;

const int mod = 1000000007;
const double pi = 3.1415926535;
const int maxn = 200001;

vec<int> v(maxn);
bit<maxn> b;

int main() {
  int n;
  scanf("%d", &n);
  vec<int> a(n);
  rep(i, n) {
    scanf("%d", &a[i]);
    v[a[i]]++;
  }
  auto m = *max_element(all(a));
  rep(i, n) {
    if (v[a[i]] > 1 && !b[a[i]])
      b[a[i]] = true;
    for (int j = a[i] * 2; j <= m; j += a[i])
      if (!b[j])
        b[j] = true;
  }
  int ans = 0;
  rep(i, n) if (!b[a[i]]) ans++;
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) begin(x), end(x)
#define gets(s) fgets(s, size(s), stdin)
using namespace std;
typedef long long ll;
typedef string str;
template <typename T> using vec = vector<T>;
template <typename T> using uset = unordered_set<T>;
template <typename T, typename S> using umap = unordered_map<T, S>;
const size_t mbs = sizeof(bitset<1>) * 8;
template <size_t N> using bit = bitset<mbs *((N + mbs - 1) / mbs)>;

const int mod = 1000000007;
const double pi = 3.1415926535;
const int maxn = 1000001;

vec<int> v(maxn);
bit<maxn> b;

int main() {
  int n;
  scanf("%d", &n);
  vec<int> a(n);
  rep(i, n) {
    scanf("%d", &a[i]);
    v[a[i]]++;
  }
  auto m = *max_element(all(a));
  rep(i, n) {
    if (v[a[i]] > 1 && !b[a[i]])
      b[a[i]] = true;
    for (int j = a[i] * 2; j <= m; j += a[i])
      if (!b[j])
        b[j] = true;
  }
  int ans = 0;
  rep(i, n) if (!b[a[i]]) ans++;
  printf("%d", ans);
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int N;
constexpr int maxA = 1000001;
vector<int> input;
vector<bool> table(maxA, true);

int main() {
  cin >> N;
  input.resize(N);
  for (int i = 0; i < N; i++)
    cin >> input[i];
  sort(input.begin(), input.end());
  for (int i = 0; i < N; i++) {
    for (int j = 2; j * input[i] < maxA; j++) {
      table[j * input[i]] = false;
    }
  }
  long long counter = 0;
  for (int i = 0; i < N; i++) {
    if (table[input[i]]) {
      if (i != 0 && input[i] == input[i - 1])
        continue;
      if (i != N - 1 && input[i] == input[i + 1])
        continue;
      counter++;
    }
  }
  cout << counter << endl;
  return 0;
}

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

int N;
constexpr int maxA = 1000001;
vector<int> input;
vector<bool> table(maxA, true);

int main() {
  cin >> N;
  input.resize(N);
  for (int i = 0; i < N; i++)
    cin >> input[i];
  sort(input.begin(), input.end());
  for (int i = 0; i < N; i++) {
    if (!table[input[i]] || (i != 0 && input[i] == input[i - 1]))
      continue;
    for (int j = 2; j * input[i] < maxA; j++) {
      table[j * input[i]] = false;
    }
  }
  long long counter = 0;
  for (int i = 0; i < N; i++) {
    if (table[input[i]]) {
      if (i != 0 && input[i] == input[i - 1])
        continue;
      if (i != N - 1 && input[i] == input[i + 1])
        continue;
      counter++;
    }
  }
  cout << counter << endl;
  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
map<lli, lli> m;
bool factors(lli n) {
  lli i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (i * i == n) {
        if (m[i] >= 1) {
          return 0;
        }
      } else {
        if (m[i] >= 1 || m[n / i] >= 1)
          return 0;
      }
    }
  }
  return 1;
}
int main() {
  fast;
  cin.tie(0);
  cout.tie(0);
  lli n;
  cin >> n;
  lli a[n], i;
  fr(i, n) {
    cin >> a[i];
    m[a[i]]++;
  }
  if (m[1] == 1) {
    cout << 1;
    return 0;
  } else if (m[1] > 1) {
    cout << 0;
    return 0;
  }
  lli count = 0;
  fr(i, n) {
    if (m[a[i]] == 1 && factors(a[i])) {
      count++;
    }
  }
  cout << count;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
lli m[1000005];
bool factors(lli n) {
  lli i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (i * i == n) {
        if (m[i] >= 1) {
          return 0;
        }
      } else {
        if (m[i] >= 1 || m[n / i] >= 1)
          return 0;
      }
    }
  }
  return 1;
}
int main() {
  fast;
  cin.tie(0);
  cout.tie(0);
  lli n;
  cin >> n;
  lli a[n], i;
  fr(i, n) {
    cin >> a[i];
    m[a[i]]++;
  }
  if (m[1] == 1) {
    cout << 1;
    return 0;
  } else if (m[1] > 1) {
    cout << 0;
    return 0;
  }
  lli count = 0;
  fr(i, n) {
    if (m[a[i]] == 1 && factors(a[i])) {
      count++;
    }
  }
  cout << count;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll mod = 1e9 + 7;

ll solve(void) {
  ll n;
  cin >> n;

  vll A(n);
  sort(A.begin(), A.end());
  for (auto &a : A) {
    cin >> a;
  }

  map<ll, ll> m;
  for (auto a : A) {
    m[a]++;
  }

  vector<bool> B(10e6 + 10, true);
  for (auto a : A) {
    if (!B[a]) {
      continue;
    }
    for (ll i = 2; i * a < B.size(); i++)
      B[i * a] = false;
  }

  ll ans = 0;
  for (auto a : A)
    ans += (m[a] == 1 && B[a] == true);

  cout << ans << endl;

  return 0;
}

int main(void) {
  while (solve()) {
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll mod = 1e9 + 7;

ll solve(void) {
  ll n;
  cin >> n;

  vll A(n);
  sort(A.begin(), A.end());
  for (auto &a : A) {
    cin >> a;
  }

  map<ll, ll> m;
  for (auto a : A) {
    m[a]++;
  }

  vector<bool> B(10e6 + 10, true);
  for (auto v : m) {
    auto a = v.first;
    if (!B[a]) {
      continue;
    }
    for (ll i = 2; i * a < B.size(); i++)
      B[i * a] = false;
  }

  ll ans = 0;
  for (auto a : A)
    ans += (m[a] == 1 && B[a] == true);

  cout << ans << endl;

  return 0;
}

int main(void) {
  while (solve()) {
  }

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  vector<int> B(100001, 0);
  for (int i = 0; i < N; i++)
    cin >> A[i], B[A[i]]++;
  int ANS = 0;
  for (int i = 0; i < N; i++) {
    bool check = true;
    B[A[i]]--;
    for (int j = 1; j * j <= A[i]; j++) {
      if (A[i] % j == 0) {
        if (B[j] != 0)
          check = false;
        if (B[A[i] / j] != 0)
          check = false;
      }
    }
    B[A[i]]++;
    if (check)
      ANS++;
  }
  cout << ANS << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  vector<int> B(1000001, 0);
  for (int i = 0; i < N; i++)
    cin >> A[i], B[A[i]]++;
  int ANS = 0;
  for (int i = 0; i < N; i++) {
    bool check = true;
    B[A[i]]--;
    for (int j = 1; j * j <= A[i]; j++) {
      if (A[i] % j == 0) {
        if (B[j] != 0)
          check = false;
        if (B[A[i] / j] != 0)
          check = false;
      }
    }
    B[A[i]]++;
    if (check)
      ANS++;
  }
  cout << ANS << endl;
}
Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  bool dp[1000005];
  rep(i, 1000005) dp[i] = false;
  map<ll, ll> mp;

  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }

  sort(all(a));

  ll ans = 0;
  rep(i, n) {
    if (dp[a[i]]) {
      continue;
    }
    if (mp[a[i]] == 1) {
      ans++;
    }

    ll num = a[i] * 2;
    while (num <= 1000001) {
      dp[num] = true;
      num += a[i];
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  bool dp[1000005];
  rep(i, 1000005) dp[i] = false;
  map<ll, ll> mp;

  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }

  sort(all(a));

  ll ans = 0;
  rep(i, n) {
    if (dp[a[i]]) {
      continue;
    }
    if (mp[a[i]] == 1) {
      ans++;
    }

    ll num = a[i];
    while (num <= 1000001) {
      dp[num] = true;
      num += a[i];
    }
  }

  cout << ans << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
int sx, sy, ex, ey;
vector<vector<int>> a;
vector<vector<int>> d;
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  vector<int> tp(m + 10, INF);
  for (int i = 1; i <= n + 10; i++)
    d.pb(tp);
  for (int i = 1; i <= n + 10; i++)
    a.pb(tp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '@');
    }
  }
  queue<pii> pq;
  pq.push(mp(sx, sy));
  d[sx][sy] = 0;
  while (pq.size()) {
    pii x = pq.front();
    pq.pop();
    int nstep = d[x.F][x.S] + 1;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j;
        if (outboard(tx, ty) || a[tx][ty])
          break;
        if (d[tx][ty] > nstep) {
          d[tx][ty] = nstep;
          pq.push(mp(tx, ty));
        }
      }
    }
  }
  if (d[ex][ey] == INF)
    puts("-1");
  else
    cout << d[ex][ey] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
int sx, sy, ex, ey;
vector<vector<int>> a;
vector<vector<int>> d;
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  vector<int> tp(m + 10, INF);
  for (int i = 1; i <= n + 10; i++)
    d.pb(tp);
  for (int i = 1; i <= n + 10; i++)
    a.pb(tp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '@');
    }
  }
  queue<pii> pq;
  pq.push(mp(sx, sy));
  d[sx][sy] = 0;
  while (pq.size()) {
    pii x = pq.front();
    pq.pop();
    int nstep = d[x.F][x.S] + 1;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j;
        if (outboard(tx, ty) || a[tx][ty] || d[tx][ty] < nstep)
          break;
        if (d[tx][ty] > nstep) {
          d[tx][ty] = nstep;
          pq.push(mp(tx, ty));
        }
      }
    }
  }
  if (d[ex][ey] == INF)
    puts("-1");
  else
    cout << d[ex][ey] << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: 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 rep2(i, i0, n) for (int i = i0; i < n; ++i)

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, x2, y1, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> c(h, vector<bool>(w, false));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '@')
        c[i][j] = true;
    }
  }

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<pair<int, int>, int>> que;

  que.push({{x1, y1}, 0});
  d[y1][x1] = 0;

  while (!que.empty()) {
    int cx = que.front().first.first;
    int cy = que.front().first.second;
    int cd = que.front().second;
    que.pop();

    if (cx == x2 && cy == y2) {
      cout << cd << endl;
      return 0;
    }

    int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    rep(i, 4) {
      rep2(j, 1, k + 1) {
        int nx = cx + j * shift[i][0];
        int ny = cy + j * shift[i][1];
        int nd = cd + 1;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          break;
        if (c[ny][nx])
          break;
        if (d[ny][nx] >= 0)
          continue;

        que.push({{nx, ny}, nd});
        d[ny][nx] = nd;
      }
    }
  }

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

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 rep2(i, i0, n) for (int i = i0; i < n; ++i)

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, x2, y1, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> c(h, vector<bool>(w, false));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '@')
        c[i][j] = true;
    }
  }

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<pair<int, int>, int>> que;

  que.push({{x1, y1}, 0});
  d[y1][x1] = 0;

  while (!que.empty()) {
    int cx = que.front().first.first;
    int cy = que.front().first.second;
    int cd = que.front().second;
    que.pop();

    if (cx == x2 && cy == y2) {
      cout << cd << endl;
      return 0;
    }

    int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    rep(i, 4) {
      rep2(j, 1, k + 1) {
        int nx = cx + j * shift[i][0];
        int ny = cy + j * shift[i][1];
        int nd = cd + 1;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          break;
        if (c[ny][nx])
          break;
        if (d[ny][nx] >= 0 && d[ny][nx] <= cd)
          break;
        if (d[ny][nx] < 0 || d[ny][nx] > nd) {
          que.push({{nx, ny}, nd});
          d[ny][nx] = nd;
        }
      }
    }
  }

  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 *  Lang    : C++
 *  Author  : little_angel
 */
#include <bits/stdc++.h>

using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  const int inf = 1e9;
  vector<vector<int>> stroke(H, vector<int>(W, inf));
  vector<string> g(H);
  int sx, sy, ex, ey, dx, dy, mx, my, x, y;
  cin >> sx >> sy >> ex >> ey;
  for (auto &i : g)
    cin >> i;
  queue<pair<int, int>> q;
  q.push({sx - 1, sy - 1});
  stroke[sx - 1][sy - 1] = 0;

  while (not q.empty()) {
    tie(x, y) = q.front();
    q.pop();
    for (auto diff : del) {
      tie(dx, dy) = diff;
      for (int k = 1; k <= K; ++k) {
        mx = x + k * dx;
        my = y + k * dy;
        if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@')
          break;
        if (stroke[mx][my] > 1 + stroke[x][y]) {
          stroke[mx][my] = 1 + stroke[x][y];
          if (mx == ex - 1 and my == ey - 1) {
            cout << stroke[mx][my] << endl;
            return 0;
          }
          q.push({mx, my});
        }
      }
    }
  }

  cout << -1 << endl;
}

Fixed submission:
/*
 *  Lang    : C++
 *  Author  : little_angel
 */
#include <bits/stdc++.h>

using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  const int inf = 1e9;
  vector<vector<int>> stroke(H, vector<int>(W, inf));
  vector<string> g(H);
  int sx, sy, ex, ey, dx, dy, mx, my, x, y;
  cin >> sx >> sy >> ex >> ey;
  for (auto &i : g)
    cin >> i;
  queue<pair<int, int>> q;
  q.push({sx - 1, sy - 1});
  stroke[sx - 1][sy - 1] = 0;

  while (not q.empty()) {
    tie(x, y) = q.front();
    q.pop();
    for (auto diff : del) {
      tie(dx, dy) = diff;
      for (int k = 1; k <= K; ++k) {
        mx = x + k * dx;
        my = y + k * dy;
        if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@' or
            1 + stroke[x][y] > stroke[mx][my])
          break;
        if (stroke[mx][my] > 1 + stroke[x][y]) {
          stroke[mx][my] = 1 + stroke[x][y];
          if (mx == ex - 1 and my == ey - 1) {
            cout << stroke[mx][my] << endl;
            return 0;
          }
          q.push({mx, my});
        }
      }
    }
  }

  cout << -1 << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, 0, h) rep(j, 0, w) cin >> g[i][j];
  vector<vector<int>> dist(h, vector<int>(w, inf));
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    auto [nx, ny] = que.front();
    que.pop();
    rep(i, 0, 4) rep(add, 1, k + 1) {
      int tx = nx + dx[i] * add, ty = ny + dy[i] * add;
      if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@')
        break;
      if (dist[tx][ty] == inf) {
        dist[tx][ty] = dist[nx][ny] + 1;
        que.push(P(tx, ty));
      }
    }
  }
  if (dist[x2][y2] == inf)
    puts("-1");
  else
    cout << dist[x2][y2] << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, 0, h) rep(j, 0, w) cin >> g[i][j];
  vector<vector<int>> dist(h, vector<int>(w, inf));
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    auto [nx, ny] = que.front();
    que.pop();
    rep(i, 0, 4) rep(add, 1, k + 1) {
      int tx = nx + dx[i] * add, ty = ny + dy[i] * add;
      if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@')
        break;
      if (dist[tx][ty] <= dist[nx][ny])
        break;
      if (dist[tx][ty] == inf) {
        dist[tx][ty] = dist[nx][ny] + 1;
        que.push(P(tx, ty));
      }
    }
  }
  if (dist[x2][y2] == inf)
    puts("-1");
  else
    cout << dist[x2][y2] << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// #define int long long

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  --a, --b, --c, --d;
  int l[h][w];
  memset(l, -1, sizeof l);
  char g[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> g[i][j];
  }
  queue<pair<int, int>> q;
  q.push({a, b}), l[a][b] = 0;
  while (!q.empty()) {
    a = q.front().first, b = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = a + j * dx[i], y = b + j * dy[i];
        if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@')
          break;
        if (l[x][y] == -1) {
          q.push({x, y});
          l[x][y] = l[a][b] + 1;
        }
        l[x][y] = min(l[x][y], l[a][b] + 1);
      }
    }
  }
  cout << l[c][d];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int long long

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  --a, --b, --c, --d;
  int l[h][w];
  memset(l, -1, sizeof l);
  char g[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> g[i][j];
  }
  queue<pair<int, int>> q;
  q.push({a, b}), l[a][b] = 0;
  while (!q.empty()) {
    a = q.front().first, b = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = a + j * dx[i], y = b + j * dy[i];
        if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@')
          break;
        if (l[x][y] == -1) {
          q.push({x, y});
          l[x][y] = l[a][b] + 1;
        }
        if (l[x][y] < l[a][b] + 1)
          break;
        l[x][y] = min(l[x][y], l[a][b] + 1);
      }
    }
  }
  cout << l[c][d];
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();

    rep(d, 4) {
      rep(i, k) {
        int next_x = x + (i + 1) * dx[d];
        int next_y = y + (i + 1) * dy[d];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (field[next_x][next_y] == '@')
          break;
        if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1)
          break;

        que.push(make_pair(next_x, next_y));
        dist[next_x][next_y] = dist[x][y] + 1;
      }
    }
  }

  /*for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
          cout << dist[i][j] << ' ';
      }
      cout << endl;
  }*/

  cout << dist[x2][y2] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();

    rep(d, 4) {
      rep(i, k) {
        int next_x = x + (i + 1) * dx[d];
        int next_y = y + (i + 1) * dy[d];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (field[next_x][next_y] == '@')
          break;
        if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1)
          break;
        if (dist[next_x][next_y] != -1)
          continue;

        que.push(make_pair(next_x, next_y));
        dist[next_x][next_y] = dist[x][y] + 1;
      }
    }
  }

  /*for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
          cout << dist[i][j] << ' ';
      }
      cout << endl;
  }*/

  cout << dist[x2][y2] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

const ll INF = 1e18;

int main() {
  int h, w, k, x, y, nx, ny, K;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, 1, -1};
  vector<vector<ll>> depth(h, vector<ll>(w, INF));
  queue<P> q;
  q.push(make_pair(b, a));
  depth[a][b] = 0;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x == d and y == c)
      break;
    rep(i, 4) {
      nx = x;
      ny = y;
      K = k;
      while (K--) {
        nx += dx[i];
        ny += dy[i];
        if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and
            depth[ny][nx] > depth[y][x]) {
          depth[ny][nx] = depth[y][x] + 1;
          q.push(make_pair(nx, ny));
        } else
          break;
      }
    }
  }
  if (depth[c][d] == INF)
    cout << -1 << endl;
  else
    cout << depth[c][d] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

const ll INF = 1e18;

int main() {
  int h, w, k, x, y, nx, ny, K;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, 1, -1};
  vector<vector<ll>> depth(h, vector<ll>(w, INF));
  queue<P> q;
  q.push(make_pair(b, a));
  depth[a][b] = 0;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x == d and y == c)
      break;
    rep(i, 4) {
      nx = x;
      ny = y;
      K = k;
      while (K--) {
        nx += dx[i];
        ny += dy[i];
        if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and
            depth[ny][nx] > depth[y][x]) {
          if (depth[ny][nx] != depth[y][x] + 1) {
            depth[ny][nx] = depth[y][x] + 1;
            q.push(make_pair(nx, ny));
          }
        } else
          break;
      }
    }
  }
  if (depth[c][d] == INF)
    cout << -1 << endl;
  else
    cout << depth[c][d] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9));
  for (int i = 0; i < w + 2; i++) {
    d[0][i] = d[h + 1][i] = 0;
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    d[i][0] = d[i][w + 1] = 0;
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        d[i][j + 1] = 0;
      }
    }
  }
  d[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  vector<vector<bool>> was(h + 2, vector<bool>(w + 2));
  was[sx][sy] = true;
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    for (int kk = 0; kk < 4; kk++) {
      int i = x, j = y;
      for (int l = 0; l < k; l++) {
        i += dx[kk], j += dy[kk];
        if (d[i][j] <= d[x][y]) {
          break;
        }
        if (was[i][j]) {
          continue;
        }
        d[i][j] = d[x][y] + 1;
        if (i == tx && j == ty) {
          cout << d[i][j] << '\n';
          return 0;
        }
        que.emplace(i, j);
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9));
  for (int i = 0; i < w + 2; i++) {
    d[0][i] = d[h + 1][i] = 0;
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    d[i][0] = d[i][w + 1] = 0;
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        d[i][j + 1] = 0;
      }
    }
  }
  d[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  vector<vector<bool>> was(h + 2, vector<bool>(w + 2));
  was[sx][sy] = true;
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    for (int kk = 0; kk < 4; kk++) {
      int i = x, j = y;
      for (int l = 0; l < k; l++) {
        i += dx[kk], j += dy[kk];
        if (d[i][j] <= d[x][y]) {
          break;
        }
        if (was[i][j]) {
          continue;
        }
        was[i][j] = true;
        d[i][j] = d[x][y] + 1;
        if (i == tx && j == ty) {
          cout << d[i][j] << '\n';
          return 0;
        }
        que.emplace(i, j);
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

int main() {

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  vvi dist(h, vi(w, INF));
  dist[sy][sx] = 0;
  queue<P> q;
  q.push(P(sy, sx));

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    vi dy = {0, 0, 1, -1};
    vi dx = {1, -1, 0, 0};
    rep(i, 4) rep1(j, k) {
      int ey, ex;
      ey = y + dy[i] * j;
      ex = x + dx[i] * j;
      if (ey < 0 || h <= ey)
        break;
      if (ex < 0 || w <= ex)
        break;
      if (s[ey][ex] == '@')
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1)
        continue;
      dist[ey][ex] = dist[y][x] + 1;
      q.push(P(ey, ex));
    }
  }

  if (dist[gy][gx] == INF)
    dist[gy][gx] = -1;
  cout << dist[gy][gx] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

int main() {

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  vvi dist(h, vi(w, INF));
  dist[sy][sx] = 0;
  queue<P> q;
  q.push(P(sy, sx));

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    vi dy = {0, 0, 1, -1};
    vi dx = {1, -1, 0, 0};
    rep(i, 4) rep1(j, k) {
      int ey, ex;
      ey = y + dy[i] * j;
      ex = x + dx[i] * j;
      if (ey < 0 || h <= ey)
        break;
      if (ex < 0 || w <= ex)
        break;
      if (s[ey][ex] == '@')
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] < dist[y][x] + 1)
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1)
        continue;
      dist[ey][ex] = dist[y][x] + 1;
      q.push(P(ey, ex));
    }
  }

  if (dist[gy][gx] == INF)
    dist[gy][gx] = -1;
  cout << dist[gy][gx] << endl;
}