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

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

const auto CHARS = 64;
using Bit = bitset<800>;

int to_i(char c) {
  if (isdigit(c))
    return c - '0';
  if (islower(c))
    return c - 'a' + 10;
  if (isupper(c))
    return c - 'A' + 36;
  return 63;
}

int main() {
  int AW, AH, BW, BH;
  cin >> AW >> AH >> BW >> BH;
  vector<string> a(AH), b(BH);
  for (auto v : {&a, &b})
    for (auto &i : *v)
      cin >> i;

  vector<vector<int>> A(AH), B(BH);
  for (auto i = 0; i < AH; ++i)
    for (auto c : a[i])
      A[i].emplace_back(to_i(c));
  for (auto i = 0; i < BH; ++i)
    for (auto c : b[i])
      B[i].emplace_back(to_i(c));

  vector<vector<Bit>> T(CHARS, vector<Bit>(AH));
  for (auto i = 0; i < AH; ++i)
    for (auto j = 0; j < AW; ++j) {
      if (A[i][j] == to_i('?'))
        for (auto &k : T)
          k[i].set(j);
      else
        T[A[i][j]][i].set(j);
    }

  vector<Bit> ok(AH);
  for (auto &i : ok)
    i.set();
  for (auto i = 0; i < AH - BH + 1; ++i) {
    for (auto y = 0; y < BH; ++y)
      for (auto x = 0; x < BW; ++x) {
        auto c = B[y][x];
        if (c == to_i('?'))
          continue;
        ok[i] &= T[c][i + y] >> x;
      }
  }

  auto res = 0;
  for (auto i = 0; i < AH - BH + 1; ++i)
    for (auto j = 0; j < AW - BW + 1; ++j)
      if (ok[i][j])
        ++res;
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

const auto WORDS = 64;
const auto BITS = 800;
using Bit = bitset<BITS>;

int toi(char c) {
  if (isdigit(c))
    return c - '0';
  if (islower(c))
    return c - 'a' + 10;
  if (isupper(c))
    return c - 'A' + 36;
  return 63;
}

int main() {
  int AW, AH, BW, BH;
  cin >> AW >> AH >> BW >> BH;
  vector<string> a(AH), b(BH);
  for (auto v : {&a, &b})
    for (auto &i : *v)
      cin >> i;

  Bit MASK;
  for (auto i = BW; i < BITS; ++i)
    MASK.set(i);

  vector<vector<Bit>> S(WORDS, vector<Bit>(AH));
  vector<vector<Bit>> T(WORDS, vector<Bit>(BH));

  for (auto c = 0; c < WORDS; ++c)
    for (auto i = 0; i < AH; ++i) {
      S[c][i].set();
      for (auto j = 0; j < AW; ++j)
        if (toi(a[i][j]) != c && toi(a[i][j]) != 63)
          S[c][i].reset(j);
    }

  for (auto c = 0; c < WORDS; ++c)
    for (auto i = 0; i < BH; ++i) {
      T[c][i].set();
      for (auto j = 0; j < BW; ++j)
        if (toi(b[i][j]) != c && toi(b[i][j]) != 63)
          T[c][i].reset(j);
    }

  auto res = 0;
  for (auto i = 0; i < AH - BH + 1; ++i)
    for (auto j = 0; j < AW - BW + 1; ++j) {
      auto ok = true;
      for (auto k = 0; k < BH; ++k) {
        Bit match;
        for (auto c = 0; c < WORDS; ++c)
          match |= (S[c][i + k] >> j) & T[c][k];
        match |= MASK;
        ;
        if (match.all())
          continue;
        ok = false;
        break;
      }
      if (ok)
        ++res;
    }
  cout << res << endl;
}
Problem p00373 (C++) — buggy submission, verdict: Runtime Error

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

int w1, w2, h1, h2, sum;
int hash_a[800][800][13], hash_b[800][13];
int cumo_a[800][800][13], cumo_b[800][13];
char s[800][800], t[800][700];

void make_table() {
  rep(i, h1) rep(j, w1 - w2 + 1) {
    rep(k, w2) {
      rep(l, 8) hash_a[i][j][(k * 8) / 64] =
          hash_a[i][j][(k * 8) / 64] * 2 + (((s[i][j + k] >> l) & 1) ? 1 : 0);
      rep(l, 8) cumo_a[i][j][(k * 8) / 64] =
          cumo_a[i][j][(k * 8) / 64] * 2 + (s[i][j + k] == '?' ? 1 : 0);
    }
  }
  rep(i, h2) {
    rep(j, w2) {
      rep(k, 8) hash_b[i][(j * 8) / 64] =
          hash_b[i][(j * 8) / 64] * 2 + (((t[i][j] >> k) & 1) ? 1 : 0);
      rep(k, 8) cumo_b[i][(j * 8) / 64] =
          cumo_b[i][(j * 8) / 64] * 2 + (t[i][j] == '?' ? 1 : 0);
    }
  }
}

void count_s() {
  rep(i, h1 - h2 + 1) {
    rep(j, w1 - w2 + 1) {
      rep(k, h2) {
        int p1[13] = {}, p2[13] = {}, flag = 0;
        rep(l, 13) {
          p1[l] = hash_a[i + k][j][l] ^ hash_b[k][l];
          p2[l] = cumo_a[i + k][j][l] | cumo_b[k][l];
          if ((p1[l] | p2[l]) != p2[l]) {
            flag++;
            break;
          }
        }
        if (flag)
          break;
        if (k == h2 - 1)
          sum++;
      }
    }
  }
}

signed main() {
  cin >> w1 >> h1 >> w2 >> h2;
  rep(i, h1) scanf("%s", s[i]);
  rep(i, h2) scanf("%s", t[i]);
  make_table();
  count_s();
  cout << sum << endl;
}

Error: 127
/tmp/08b1aafe-9a13-49cb-a982-a2f470c8ca35.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object


Fixed submission:

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

int w1, w2, h1, h2, sum;
int hash_a[700][700][13], hash_b[700][13];
int cumo_a[700][700][13], cumo_b[700][13];
char s[700][700], t[700][700];

void make_table() {
  rep(i, h1) rep(j, w1 - w2 + 1) {
    rep(k, w2) {
      rep(l, 8) hash_a[i][j][(k * 8) / 64] =
          hash_a[i][j][(k * 8) / 64] * 2 + (((s[i][j + k] >> l) & 1) ? 1 : 0);
      rep(l, 8) cumo_a[i][j][(k * 8) / 64] =
          cumo_a[i][j][(k * 8) / 64] * 2 + (s[i][j + k] == '?' ? 1 : 0);
    }
  }
  rep(i, h2) {
    rep(j, w2) {
      rep(k, 8) hash_b[i][(j * 8) / 64] =
          hash_b[i][(j * 8) / 64] * 2 + (((t[i][j] >> k) & 1) ? 1 : 0);
      rep(k, 8) cumo_b[i][(j * 8) / 64] =
          cumo_b[i][(j * 8) / 64] * 2 + (t[i][j] == '?' ? 1 : 0);
    }
  }
}

void count_s() {
  rep(i, h1 - h2 + 1) {
    rep(j, w1 - w2 + 1) {
      rep(k, h2) {
        int p1[13] = {}, p2[13] = {}, flag = 0;
        rep(l, 13) {
          p1[l] = hash_a[i + k][j][l] ^ hash_b[k][l];
          p2[l] = cumo_a[i + k][j][l] | cumo_b[k][l];
          if ((p1[l] | p2[l]) != p2[l]) {
            flag++;
            break;
          }
        }
        if (flag)
          break;
        if (k == h2 - 1)
          sum++;
      }
    }
  }
}

signed main() {
  cin >> w1 >> h1 >> w2 >> h2;
  rep(i, h1) scanf("%s", s[i]);
  rep(i, h2) scanf("%s", t[i]);
  make_table();
  count_s();
  cout << sum << endl;
}
Problem p00373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
vector<int> match(string &t, string &p) {
  // cout<<t<<" "<<p<<endl;
  using BS = bitset<100>;
  int n = t.length(), m = p.length();
  vector<BS> M(100);
  vector<int> res(n, 0);
  for (int i = 0; i < m; i++) {
    if (p[i] == '?')
      for (int j = 0; j < 100; j++)
        M[j][i] = 1;
    if (isdigit(p[i]))
      M[p[i] - '0'][i] = 1;
    if (islower(p[i]))
      M[10 + p[i] - 'a'][i] = 1;
    if (isupper(p[i]))
      M[36 + p[i] - 'A'][i] = 1;
  }
  BS S(0);
  for (int i = 0; i < n; i++) {
    S = ((S << 1) | BS(1));
    if (isdigit(t[i]))
      S &= M[t[i] - '0'];
    if (islower(t[i]))
      S &= M[10 + t[i] - 'a'];
    if (isupper(t[i]))
      S &= M[36 + t[i] - 'A'];

    if ((S & (BS(1) << (m - 1))).count())
      res[i] = 1;
  }
  // for(int i=0;i<n;i++) cout<<res[i];cout<<endl;
  return res;
}

signed main() {
  int aw, ah, bw, bh;
  cin >> aw >> ah >> bw >> bh;
  string a[ah], b[bh];
  for (int i = 0; i < ah; i++)
    cin >> a[i];
  for (int i = 0; i < bh; i++)
    cin >> b[i];
  // for(int i=0;i<ah;i++) cout<<a[i]<<endl;;
  // for(int i=0;i<bh;i++) cout<<b[i]<<endl;;
  vector<int> G[ah][bh];
  for (int i = 0; i < ah; i++)
    for (int j = 0; j < bh; j++)
      G[i][j] = match(a[i], b[j]);

  int ans = 0;
  for (int i = 0; i <= ah - bh; i++) {
    for (int j = 0; j < aw; j++) {
      int res = 1;
      for (int k = 0; k < bh; k++)
        res &= G[i + k][k][j];
      ans += res;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
vector<int> match(string &t, string &p) {
  // cout<<t<<" "<<p<<endl;
  using BS = bitset<256>;
  int n = t.length(), m = p.length();
  vector<BS> M(256);
  vector<int> res(n, 0);
  for (int i = 0; i < m; i++) {
    if (p[i] != '?')
      M[p[i]][i] = 1;
    else
      for (int j = 0; j < 256; j++)
        M[j][i] = 1;
  }
  BS S(0);
  for (int i = 0; i < n; i++) {
    S = ((S << 1) | BS(1));
    if (t[i] != '?')
      S &= M[t[i]];
    if ((S & (BS(1) << (m - 1))).count())
      res[i] = 1;
  }
  // for(int i=0;i<n;i++) cout<<res[i];cout<<endl;
  return res;
}

signed main() {
  int aw, ah, bw, bh;
  cin >> aw >> ah >> bw >> bh;
  string a[ah], b[bh];
  for (int i = 0; i < ah; i++)
    cin >> a[i];
  for (int i = 0; i < bh; i++)
    cin >> b[i];
  // for(int i=0;i<ah;i++) cout<<a[i]<<endl;;
  // for(int i=0;i<bh;i++) cout<<b[i]<<endl;;
  vector<int> G[ah][bh];
  for (int i = 0; i < ah; i++)
    for (int j = 0; j < bh; j++)
      G[i][j] = match(a[i], b[j]);

  int ans = 0;
  for (int i = 0; i <= ah - bh; i++) {
    for (int j = 0; j < aw; j++) {
      int res = 1;
      for (int k = 0; k < bh; k++)
        res &= G[i + k][k][j];
      ans += res;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00379 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  long long a, n, m, R_ans, L_sum;
  int ans = 0, i = 1;

  cin >> a >> n >> m;

  R_ans = powl(i + a, n);

  while (R_ans <= m) {
    int num = R_ans;
    L_sum = 0;

    //  各桁数の合計値を求める
    while (num > 0) {
      L_sum += num % 10;
      num /= 10;
    }

    //  左辺と右辺の式を比較
    if (R_ans == powl(L_sum + a, n))
      ans++;
    i++;
    R_ans = powl(i + a, n);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int a, n, m, R, L_sum;
  int ans = 0, i = 1;

  cin >> a >> n >> m;

  R = powl(i + a, n);

  while (R <= m) {
    int num = R;
    L_sum = 0;

    //  各桁数の合計値を求める
    while (num > 0) {
      L_sum += num % 10;
      num /= 10;
    }

    //  左辺と右辺の式を比較
    if (R == powl(L_sum + a, n))
      ans++;
    i++;
    R = powl(i + a, n);
  }

  cout << ans << endl;

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

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

int main() {

  int a, n, m, L_sum;
  int ans = 0, i = 1;
  long long R_ans;
  cin >> a >> n >> m;

  R_ans = powl(i + a, n);

  while (R_ans <= m) {
    int num = R_ans;
    L_sum = 0;

    //  各桁数の合計値を求める
    while (num > 0) {
      L_sum += num % 10;
      num /= 10;
    }

    //  左辺と右辺の式を比較
    if (R_ans == powl(L_sum + a, n))
      ans++;
    i++;
    R_ans = powl(i + a, n);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int a, n, m, R_ans, L_sum;
  int ans = 0, i = 1;

  cin >> a >> n >> m;

  R_ans = powl(i + a, n);

  while (R_ans <= m) {
    int num = R_ans;
    L_sum = 0;

    //  各桁数の合計値を求める
    while (num > 0) {
      L_sum += num % 10;
      num /= 10;
    }

    //  左辺と右辺の式を比較
    if (R_ans == powl(L_sum + a, n))
      ans++;
    i++;
    R_ans = powl(i + a, n);
  }

  cout << ans << endl;

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

#include <cmath>
#include <iostream>
using namespace std;
int main(void) {

  // 変数：デュードニー数に足す値「 a 」、指数「 n 」、上限の数「 m 」と置く
  int a, n, m, ans = 0;

  // 変数：右辺の答え「 rightAns 」、右辺の各桁の合計値「 rightDigitSum
  // 」、左辺の合計値「 leftSum 」、と置く
  int rightAns = 0, rightDigitSum = 0, leftSum = 1;
  ;

  // 変数：左辺の合計値を求めるための仮の数「 rightSum 」と置く
  int rightSum;

  // 入力：デュードニー数に足す値「 a 」、指数「 n 」、上限の数「 m 」を入力する
  cin >> a >> n >> m;

  while (pow(leftSum + a, n) <= m) {
    // 代入:右辺の答え「 rightAns 」に(y + a)^2の値を代入
    rightAns = pow(leftSum + a, n);
    rightSum = rightAns;
    rightDigitSum = 0;

    while (rightSum) {
      rightDigitSum += (rightSum % 10);
      rightSum /= 10;
    }

    if (rightAns == pow(rightDigitSum + a, n))
      ans++;

    leftSum++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int a, n, m;
  int ans = 0, sum = 0;

  cin >> a >> n >> m;

  long long kai = powl(a, n) + 1;

  for (kai; kai <= m; kai++) {
    if (kai == 100000000)
      sum = kai / 100000000;
    else if (kai > 10000000) {
      sum = sum + kai / 10000000;
      kai %= 10000000;
    } else if (kai > 1000000) {
      sum = sum + kai / 1000000;
      kai %= 1000000;
    } else if (kai > 100000) {
      sum = sum + kai / 100000;
      kai %= 100000;
    } else if (kai > 10000) {
      sum = sum + kai / 10000;
      kai %= 10000;
    } else if (kai > 1000) {
      sum = sum + kai / 1000;
      kai %= 1000;
    } else if (kai > 100) {
      sum = sum + kai / 100;
      kai %= 100;
    } else if (kai > 10) {
      sum = sum + kai / 10;
      kai %= 10;
    } else if (kai > 1) {
      sum = sum + kai / 1;
    } else if (kai - powl(a, n) == sum * (sum + 2 * a))
      ans++;
  }

  cout << ans << endl;

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

/*
生の整数xのの各桁をすべて足して得られる数の3乗がxになるとき、この数xをデュードニー数といいます。たとえば、
512は8の3乗で、5+1+2=8となるので512はデュードニー数です。
この問題では、デュードニー数と似た数を考えて、その個数をもとめます。
負でない整数a、2以上の整数n、および上限値mが与えられる。このとき、xの各桁をすべて足して得られる数についてyについて、
x=(y+a)n乗となるm以下の正の整数xの個数を入力するプログラムを作りなさい。
<入力>
a n m
0<=a<=50
2<=n<=10
1000<=m<=100000000(1e8)
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
long long val(long long x) {
  long long sum = 0;
  while (x) {
    sum += x % 10;
    x /= 10;
  }
  return sum;
}
int main(void) {
  long long a, n, m;
  cin >> a >> n >> m;
  int cnt = 0;
  for (int y = 1; y <= 72; y++) {
    long long x = 1;
    for (int t = 1; t <= n; t++) {
      x *= (y + a);
    }
    if (x <= m && val(x) == y)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
long long a = 0, n = 0, m = 0, ch = 1, c = 0, k = 0;
int main(void) {
  cin >> a >> n >> m;
  for (int i = 1; i < m; i++) {
    k = i / 100000000 + i % 100000000 / 10000000 + i % 10000000 / 1000000 +
        i % 1000000 / 100000 + i % 100000 / 10000 + i % 10000 / 1000 +
        i % 1000 / 100 + i % 100 / 10 + i % 10 + a;
    for (int j = 0; j < n; j++) {
      ch *= k;
    }
    // cout<<"i("<<i<<")=ch("<<ch<<")"<<endl;
    if (i == ch) {
      c++;
    }
    ch = 1;
  }
  cout << c << endl;
  return 0;
}
Problem p00380 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

// using lli = long long int;
int inf = 1000000007;
using namespace std;
#define sort(X) sort(X.begin(), X.end())

int N;
int Q;
int x, y;

int main() {
  cin >> N;
  vector<int> a(N);
  vector<int> b(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b);

  // 整列した場合との一致度
  int match = 0;
  for (int i = 0; i < N; i++)
    match += (a[i] == b[i] ? 1 : 0);
  // 既に整列している
  if (match == N) {
    cout << 0 << endl;
    return 0;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> x >> y;
    x -= 1;
    y -= 1;
    // 揃っていたものをSwapしてしまう場合
    if (b[x] == a[x])
      match -= 1;
    if (b[y] == a[y])
      match -= 1;
    int c = a[x];
    a[x] = a[y];
    a[y] = c;
    // もしSwapした時に揃った場合
    if (b[x] == a[x])
      match += 1;
    if (b[y] == a[y])
      match += 1;

    // 揃った数 = 数列の長さなら全て整列したことになる
    if (match == N) {
      cout << (i + 1) << endl;
      return 0;
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int CheckSort(int c);
int a[300000] = {0};

int main() {

  int N, Q, x, y, ANS = 1;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  /* 初めにソートするか確認 */
  if (CheckSort(N)) {
    ANS = 0;
    cout << ANS << endl;
    return 0;
  }

  /* ボアソートして回数を確認 */
  cin >> Q;
  for (int j = 0; j < Q; j++) {
    cin >> x >> y;
    if (a[x] >= a[y])
      swap(a[x], a[y]);
    if (!CheckSort(N))
      ANS++;
  }

  /* ボアソートが出来なかった場合 */
  if (!CheckSort(N)) {
    ANS = -1;
    cout << ANS << endl;
    return 0;
  }

  cout << ANS << endl;

  return 0;
}

int CheckSort(int z) {

  for (int i = 1; i <= z; i++) {
    for (int j = i + 1; j <= z; j++) {
      if (a[i] > a[j])
        return false;
    }
  }
  return true;
}
Problem p00381 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MOD 1000000007
#define N_MAX 100001

int main() {
  int dp[27] = {};
  char enter[N_MAX], exit[N_MAX];
  int N;
  std::cin >> N;
  std::cin >> enter >> exit;
  dp[enter[0] - 'a'] += 1;
  for (int i = 1; i < N - 1; i++) {
    dp[enter[i] - 'a'] += dp[exit[i] - 'a'];
    dp[enter[i] - 'a'] %= MOD;
  }
  std::cout << dp[exit[N - 1] - 'a'] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

#define DB(x)

std::vector<int> find_all(const std::string str, const char subStr) {
  std::vector<int> result;

  int pos = str.find(subStr);

  while (pos != std::string::npos) {
    result.push_back(pos);
    pos = str.find(subStr, pos + 1);
  }

  return result;
}

int main() {
  int N = 0;
  std::cin >> N;
  std::string enter, exit, buffer;
  std::cin >> buffer;
  enter.push_back(' ');
  enter += buffer;
  std::cin >> buffer;
  exit.push_back(' ');
  exit += buffer;
  // OK
  unsigned long long dp[N + 1]; // zero to N(int)
  for (int i = 0; i <= N; i++) {
    dp[i] = 0;
  }
  dp[1] = 1;
  for (int i = 1; i <= N; i++) {
    DB(std::cout << "loop" << i << std::endl);
    if (dp[i] == 0)
      continue;
    else {
      std::vector<int> same_char = find_all(exit, enter[i]);
      for (int k = 0; k < same_char.size(); k++) {
        DB(std::cout << "where:" << same_char[k] << std::endl);
        if (i < same_char[k]) {
          dp[same_char[k]] += dp[i];
          dp[same_char[k]] %= 1000000007;
          DB(std::cout << dp[same_char[k]] << std::endl);
        }
      }
    }
  }
  std::cout << dp[N] % 1000000007 << std::endl;
}
Problem p00383 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};

namespace std {
bool operator<(const P &a, const P &b) {
  return !EQ(a.X, b.X) ? a.X < b.X : a.Y + EPS < b.Y;
}
bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }
} // namespace std
P unit(const P &p) { return p / abs(p); }

int main() {
  int n, k;
  cin >> n >> k;
  k--;
  VP p(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  bool over = false;
  for (int i = 0; i < n - k and !over; i++) {
    map<P, int> dirs;
    for (int j = i + 1; j < n; j++) {
      P dir = p[i] - p[j];
      if (dir.X < 0 or (dir.X == 0 and dir.Y < 0))
        dir = -dir;
      dir = unit(dir);
      dirs[dir]++;
    }
    for (auto e : dirs) {
      if (e.second >= k) {
        over = true;
        break;
      }
    }
  }
  cout << over << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};

namespace std {
bool operator<(const P &a, const P &b) {
  return !EQ(a.X, b.X) ? a.X < b.X : a.Y + EPS < b.Y;
}
bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }
} // namespace std
double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
P unit(const P &p) { return p / abs(p); }
bool intersectLP(const L &l, const P &p) {
  return abs(cross(l[1] - p, l[0] - p)) < EPS;
}

int main() {
  int n, k;
  cin >> n >> k;
  k -= 2;
  VP p(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  bool over = false;
  for (int i = 0; i < n and !over; i++) {
    for (int j = i + 1; j < n - k and !over; j++) {
      L l(p[i], p[j]);
      int rem = k;
      for (int d = j + 1; d < n; d++) {
        if (n - d < rem)
          break;
        if (intersectLP(l, p[d]))
          rem--;
      }
      if (rem <= 0)
        over = true;
    }
  }
  cout << over << endl;
  return 0;
}
Problem p00392 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const double PI = acos(-1);
const double EPS = 1e-15;
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
typedef pair<int, int> P;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define mp make_pair

int par[112345];
int rk[112345];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rk[i] = 0;
  }
}

int fd(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = fd(par[x]);
  }
}

void unite(int x, int y) {
  x = fd(x);
  y = fd(y);
  if (x == y)
    return;

  if (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  init(112345);
  std::vector<int> a(n);
  REP(i, n) scanf("%d", &a[i]);

  for (int i = 2; i < 3333; ++i) {
    int pin = -1;
    REP(j, n) {
      if (a[j] % i == 0) {
        if (pin == -1) {
          pin = a[j];
        } else {
          unite(a[j], pin);
        }
      }
    }
  }

  std::vector<int> sorted(a);
  std::sort(sorted.begin(), sorted.end());

  int ans = 1;

  REP(i, n) {
    if (fd(a[i]) != fd(sorted[i])) {
      ans = 0;
    }
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

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

const double PI = acos(-1);
const double EPS = 1e-15;
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
typedef pair<int, int> P;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define mp make_pair

int par[112345];
int rk[112345];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rk[i] = 0;
  }
}

int fd(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = fd(par[x]);
  }
}

void unite(int x, int y) {
  x = fd(x);
  y = fd(y);
  if (x == y)
    return;

  if (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y])
      rk[x]++;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  init(112345);
  std::vector<int> a(n);
  REP(i, n) scanf("%d", &a[i]);

  for (int i = 2; i < 112345; ++i) {
    int pin = -1;
    REP(j, n) {
      if (a[j] % i == 0) {
        if (pin == -1) {
          pin = a[j];
        } else {
          unite(a[j], pin);
        }
      }
    }
  }

  std::vector<int> sorted(a);
  std::sort(sorted.begin(), sorted.end());

  int ans = 1;

  REP(i, n) {
    if (fd(a[i]) != fd(sorted[i])) {
      ans = 0;
    }
  }

  printf("%d\\n", ans);

  return 0;
}