alexjercan/bugnet huggingface dataset:
Problem p01772 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  string s;
  cin >> s;
  int i = 0;
  while (i < s.size()) {
    if (s[i] != 'A' && s[i] != 'Z') {
      s.erase(i, 1);
    } else {
      i++;
    }
  }
  for (int i = 0; i < s.size();) {
    if (i % 2 == 0) {
      if (s[i] != 'A') {
        s.erase(i, 1);
      } else {
        i++;
      }
    } else {
      if (s[i] != 'Z') {
        s.erase(i, 1);
      } else {
        i++;
      }
    }
    if (s.size() == 0)
      break;
  }
  if (s.size() > 0) {
    if (s[s.size() - 1] != 'Z')
      s.erase(s.size() - 1, 1);
    if (s[0] == 'A' && s[s.size() - 1] == 'Z') {
      cout << s << endl;
    } else {
      cout << "-1" << endl;
    }
  } else {
    cout << "-1" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  string s;
  cin >> s;
  int i = 0;
  while (i < s.size()) {
    if (s[i] != 'A' && s[i] != 'Z') {
      s.erase(i, 1);
    } else {
      i++;
    }
  }
  for (int i = 0; i < s.size();) {
    if (i % 2 == 0) {
      if (s[i] != 'A') {
        s.erase(i, 1);
      } else {
        i++;
      }
    } else {
      if (s[i] != 'Z') {
        s.erase(i, 1);
      } else {
        i++;
      }
    }
  }
  if (s[s.size() - 1] != 'Z')
    s.erase(s.size() - 1, 1);
  if (s[0] == 'A' && s[s.size() - 1] == 'Z') {
    cout << s << endl;
  } else {
    cout << "-1" << endl;
  }
  return 0;
}
Problem p01772 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string row;
  cin >> row;
  int f = 0, cnt = 0;
  // cout<<row.size();
  for (int i = 0; i < row.size(); i++) {
    if (row[i] == 'A')
      f = 1;
    if (f == 1 && row[i] == 'Z') {
      cnt++;
      f = 0;
    }
  }
  if (cnt != 0) {
    for (int i = 0; i < cnt; i++) {
      cout << "AZ";
    }
    cout << endl;
  } else
    cout << -1 << endl;

  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  string row;
  cin >> row;
  int f = 0, cnt = 0;
  // cout<<row.size();
  for (int i = 0; row.size(); i++) {
    if (row[i] == 'A')
      f = 1;
    if (f == 1 && row[i] == 'Z') {
      cnt++;
      f = 0;
    }
  }
  if (cnt != 0) {
    for (int i = 0; i < cnt; i++) {
      cout << "AZ";
    }
    cout << endl;
  } else
    cout << -1 << endl;

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

#include <iostream>
using namespace std;

int main() {
  char input[21];
  char po;
  int i = 0;
  int b = 0;
  int check = 0;

  while (cin >> input) {
    i = 0;
    b = 0;
    check = 0;
    while (1) {
      if (b == 0 && input[i] == 'A') {
        po = input[i];
        b = 1;
      } else if (b == 1 && input[i] == 'Z') {
        cout << po << input[i];
        check = 1;
        b = 0;
      }
      if (input[i] == '\\0')
        break;
      i++;
    }
    if (check == 0)
      cout << -1;
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  char input[21];
  int i = 0;
  int b = 0;
  int check = 0;

  while (1) {
    cin >> input;
    i = 0;
    b = 0;
    while (1) {
      if (b == 0 && input[i] == 'A') {
        if (i == 0)
          check = 1;
        cout << input[i];
        b = 1;
      } else if (b == 1 && input[i] == 'Z') {
        cout << input[i];
        b = 0;
      }
      if (input[i] == '\\0')
        break;
      i++;
    }
    if (check == 0)
      cout << -1;
    cout << endl;
  }
}
Problem p01772 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  string s, t;
  cin >> s;
  int cnt = 0, i = -1;
  while (++i < s.size())
    if (cnt & 1) {
      if (s[i] == 'Z')
        cnt++;
    } else if (s[i] == 'A')
      cnt++;
  if (cnt /= 2)
    while (cnt--)
      cout << "AZ";
  else
    cout << -1;
  cout << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  string s, t;
  cin >> s;
  int cnt = 0, i = 0;
  while (i < s.size()) {
    if (cnt & 1)
      if (s[i] == 'Z')
        cnt++;
      else if (s[i] == 'A')
        cnt++;
  }
  cnt /= 2;
  if (cnt == 0)
    cout << -1;
  else
    while (cnt--)
      cout << "AZ";
  cout << endl;
}
Problem p01772 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;

  int C = 0, ptr = 0;

  cin >> S;

  while (true) {
    if (ptr < S.size()) {
      while (S[ptr] != 'A') {
        ptr++;

        if (ptr >= S.size()) {
          goto Exit;
        }
      }
    } else {
      goto Exit;
    }

    ptr++;

    if (ptr < S.size()) {
      while (S[ptr] != 'Z') {
        ptr++;

        if (ptr >= S.size()) {
          goto Exit;
        }
      }

      C++;
    } else {
      goto Exit;
    }
  }

Exit:;

  if (C == 0) {
    cout << -1 << endl;
  } else {
    for (int i = 0; i < C; i++) {
      cout << "AZ";
    }

    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;

  int C = 0, ptr = 0;

  cin >> S;

  while (true) {
    if (ptr < S.size()) {
      while (S[ptr] != 'A') {
        ptr++;

        if (ptr >= S.size()) {
          goto Exit;
        }
      }
    }

    ptr++;

    if (ptr < S.size()) {
      while (S[ptr] != 'Z') {
        ptr++;

        if (ptr >= S.size()) {
          goto Exit;
        }
      }

      C++;
    }
  }

Exit:;

  if (C == 0) {
    cout << -1 << endl;
  } else {
    for (int i = 0; i < C; i++) {
      cout << "AZ";
    }

    cout << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

#pragma warning(disable : 4996)

#define MAX_N 120
#define MAX_M 1440

int x[MAX_N][MAX_M];
int y[MAX_N][MAX_M];
int z[MAX_N][2];
int a, n, m, b, a1, a2, a3, a4, a5, a6;

int main() {
  cin >> a;
  for (int i = 0; i < a; i++) {
    scanf("%02d:%02d-%02d:%02d", &a1, &a2, &a3, &a4);
    z[i][0] = a1 * 60 + a2;
    z[i][1] = a3 * 60 + a4;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b;
    for (int j = 0; j < b; j++) {
      scanf("%02d:%02d-%02d:%02d", &a1, &a2, &a3, &a4);
      a5 = a1 * 60 + a2;
      a6 = a3 * 60 + a4;
      for (int j = a5; j < a6; j++) {
        x[i][j] = 1;
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b;
    for (int j = 0; j < b; j++) {
      scanf("%02d:%02d-%02d:%02d", &a1, &a2, &a3, &a4);
      a5 = a1 * 60 + a2;
      a6 = a3 * 60 + a4;
      for (int j = a5; j < a6; j++) {
        y[i][j] = 1;
      }
    }
  }
  int sum = 0, sum1 = 0, sum2 = 0;
  for (int i = 0; i < a; i++) {
    sum1 = 0;
    sum2 = 0;
    for (int j = 0; j < n; j++) {
      for (int k = z[i][0]; k < z[i][1]; k++) {
        if (x[j][k] == 0) {
          goto E;
        }
      }
      sum1++;
    E:;
    }
    for (int j = 0; j < m; j++) {
      for (int k = z[i][0]; k < z[i][1]; k++) {
        if (y[j][k] == 0) {
          goto F;
        }
      }
      sum2++;
    F:;
    }
    sum += min(sum1, sum2);
  }
  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

#pragma warning(disable : 4996)

#define MAX_N 120
#define MAX_M 1440

int x[MAX_N][MAX_M];
int y[MAX_N][MAX_M];
int z[MAX_N][2];
int a, n, m, a1, a2, a3, a4, a5, a6;

int main() {
  cin >> a;
  for (int i = 0; i < a; i++) {
    scanf("%02d:%02d-%02d:%02d", a1, a2, a3, a4);
    z[i][0] = a1 * 60 + a2;
    z[i][1] = a3 * 60 + a4;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%02d:%02d-%02d:%02d", a1, a2, a3, a4);
    a5 = a1 * 60 + a2;
    a6 = a3 * 60 + a4;
    for (int j = a5; j < a6; j++) {
      x[i][j] = 1;
    }
  }
  cin >> m;
  for (int i = 0; i < n; i++) {
    scanf("%02d:%02d-%02d:%02d", a1, a2, a3, a4);
    a5 = a1 * 60 + a2;
    a6 = a3 * 60 + a4;
    for (int j = a5; j < a6; j++) {
      y[i][j] = 1;
    }
  }
  int sum = 0, sum1 = 0, sum2 = 0;
  for (int i = 0; i < a; i++) {
    sum1 = 0;
    sum2 = 0;
    for (int j = 0; j < n; j++) {
      for (int k = z[i][0]; k < z[i][1]; k++) {
        if (x[j][k] == 0) {
          goto E;
        }
      }
      sum1++;
    E:;
    }
    for (int j = 0; j < m; j++) {
      for (int k = z[i][0]; k < z[i][1]; k++) {
        if (y[j][k] == 0) {
          goto F;
        }
      }
      sum2++;
    F:;
    }
    sum += min(sum1, sum2);
  }
  cout << sum << endl;
  return 0;
}
Problem p01773 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
P s2p(string s) { return P(stoi(s.substr(0, 2)), stoi(s.substr(3, 2))); }
bool contain(string c, string p) {
  P stc = s2p(c.substr(0, 5));
  P enc = s2p(c.substr(6, 5));
  P stp = s2p(p.substr(0, 5));
  P enp = s2p(p.substr(6, 5));
  // cout<<stc.first<<" "<<stc.second<<endl;
  // cout<<enc.first<<" "<<enc.second<<endl;
  // cout<<stp.first<<" "<<stp.second<<endl;
  // cout<<enp.first<<" "<<enp.second<<endl;
  return stp <= stc && enc <= enp;
}
int main() {
  int k, n, m;
  cin >> k;
  string c[k];
  for (int i = 0; i < k; i++)
    cin >> c[i];
  cin >> n;
  vector<string> t[n];
  for (int i = 0, j; i < n; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      t[i].push_back(tmp);
    }
  }
  cin >> m;
  vector<string> s[m];
  for (int i = 0, j; i < m; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      s[i].push_back(tmp);
    }
  }
  int ans = 0;
  for (int i = 0; i < k; i++) {
    int a = 0, b = 0;
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)t[l].size(); j++) {
        f |= contain(c[i], t[l][j]);
      }
      if (f)
        a++;
    }
    for (int l = 0; l < m; l++) {
      bool f = 0;
      for (int j = 0; j < (int)s[l].size(); j++) {
        f |= contain(c[i], s[l][j]);
      }
      if (f)
        b++;
    }
    // cout<<a<<" "<<b<<endl;
    ans += min(a, b);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
P s2p(string s) { return P(stoi(s.substr(0, 2)), stoi(s.substr(3, 2))); }
bool contain(string c, string p) {
  P stc = s2p(c.substr(0, 5));
  P enc = s2p(c.substr(6, 5));
  P stp = s2p(p.substr(0, 5));
  P enp = s2p(p.substr(6, 5));
  // cout<<stc.first<<" "<<stc.second<<endl;
  // cout<<enc.first<<" "<<enc.second<<endl;
  // cout<<stp.first<<" "<<stp.second<<endl;
  // cout<<enp.first<<" "<<enp.second<<endl;
  return stp <= stc && enc <= enp;
}
int main() {
  int k, n, m;
  cin >> k;
  string c[k];
  for (int i = 0; i < k; i++)
    cin >> c[i];
  cin >> n;
  vector<string> t[n];
  for (int i = 0, j; i < n; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      t[i].push_back(tmp);
    }
  }
  cin >> m;
  vector<string> s[m];
  for (int i = 0, j; i < m; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      s[i].push_back(tmp);
    }
  }
  int ans = 0;
  for (int i = 0; i < k; i++) {
    int a = 0, b = 0;
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)t[l].size(); j++) {
        f |= contain(c[i], t[l][j]);
      }
      if (f)
        a++;
    }
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)s[l].size(); j++) {
        f |= contain(c[i], s[l][j]);
      }
      if (f)
        b++;
    }
    // cout<<a<<" "<<b<<endl;
    ans += min(a, b);
  }
  cout << ans << endl;
  return 0;
}
Problem p01773 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;

int main() {
  int a, n, m;
  scanf("%d", &a);
  vector<Pi> datas;
  for (int i = 0; i < a; i++) {
    int H1, H2, M1, M2;
    scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
    datas.push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
  }
  scanf("%d", &n);
  vector<vector<Pi>> teacher(n);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      teacher[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }
  scanf("%d", &m);
  vector<vector<Pi>> student(m);
  for (int i = 0; i < m; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      student[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }

  int ret = 0;
  for (int i = 0; i < a; i++) {
    int t = 0, s = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < teacher[j].size(); k++) {
        if (teacher[j][k].first <= datas[i].first &&
            datas[i].second <= teacher[j][k].second) {
          ++t;
          break;
        }
      }
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < student[j].size(); k++) {
        if (student[j][k].first <= datas[i].first &&
            datas[i].second <= student[j][k].second) {
          ++s;
          break;
        }
      }
    }
    ret += min(t, s);
  }
  printf("%d\\n", ret);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;

int main() {
  int a, n, m;
  scanf("%d", &a);
  vector<Pi> datas;
  for (int i = 0; i < a; i++) {
    int H1, H2, M1, M2;
    scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
    datas.push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
  }
  scanf("%d", &n);
  vector<vector<Pi>> teacher(n);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      teacher[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }
  scanf("%d", &m);
  vector<vector<Pi>> student(m);
  for (int i = 0; i < m; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      student[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }

  int ret = 0;
  for (int i = 0; i < a; i++) {
    int t = 0, s = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < teacher[j].size(); k++) {
        if (teacher[j][k].first <= datas[i].first &&
            datas[i].second <= teacher[j][k].second) {
          ++t;
          break;
        }
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < student[j].size(); k++) {
        if (student[j][k].first <= datas[i].first &&
            datas[i].second <= student[j][k].second) {
          ++s;
          break;
        }
      }
    }
    ret += min(t, s);
  }
  printf("%d\\n", ret);
}
Problem p01777 (C++) — buggy submission, verdict: Time Limit Exceeded

// O(NlogN + Qlog^2N)の適当解法(クエリ入れ替えなし)
// 意味が分からないバグに悩まされる。→セグメントツリーの子はind*2 + 1とind*2 +
// 2。間違えてたし気づかなすぎたので、頭ない。
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int d;
int x[252521];
int q;
int l[100000], r[100000], e[100000];

const int INF = 1145141919;

const int depth = 17;
class Segtree {
public:
  vector<int> data[1 << (depth + 1)];

  void ini(int index = 0, int a = 0, int b = (1 << (depth)) - 1) {
    //[a, b]の昇順ソート列をdata[index]に入れる。
    for (int i = a; i <= b; i++)
      data[index].push_back(x[i]);
    sort(data[index].begin(), data[index].end());
    if (b - a > 0) {
      ini(index * 2 + 1, a, a + (b - a - 1) / 2);
      ini(index * 2 + 2, a + (b - a + 1) / 2, b);
    }
  }

  //[l, r]にv以下の要素がいくつあるか。
  int count(int l, int r, int v, int index = 0, int a = 0,
            int b = (1 << (depth)) - 1) {
    if (a > r || b < l || data[index].size() == 0)
      return 0;

    if (l <= a && b <= r) {
      //[a,
      //b]の昇順ソート列の中で、v(以下の要素のうち最大の要素)は何番目にある？
      // 最初を0番とする。
      int st = -1, ed = data[index].size() - 1, medi;
      while (st <= ed) {
        medi = (st + ed) / 2;
        if (data[index][medi] > v)
          ed = medi - 1;
        else {
          if (medi + 1 == data[index].size() || data[index][medi + 1] > v)
            break;
          st = medi + 1;
        }
      }
      // Index + 1を返す
      return medi + 1;
    }
    int cnt1 = count(l, r, v, index * 2 + 1, a, a + (b - a - 1) / 2);
    int cnt2 = count(l, r, v, index * 2 + 2, a + (b - a + 1) / 2, b);
    return cnt1 + cnt2;
  }
} seg;

signed main() {
  int i, j;
  cin >> d;
  for (i = 0; i < d; i++)
    cin >> x[i];
  for (i = d; i < 252521; i++)
    x[i] = INF;

  seg.ini();

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> l[i] >> r[i] >> e[i];
    l[i]--;
    r[i]--;
    int a = max(x[l[i]], x[r[i]]) + e[i];
    int b = min(x[l[i]], x[r[i]]) - e[i];

    int cnt1 = seg.count(l[i], r[i], a);
    int cnt2 = seg.count(l[i], r[i], b - 1);

    int ans = r[i] - l[i] + 1 - (cnt1 - cnt2);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// まずはO(NQ)の想定TLE解法
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int d;
int x[100000];
int q;
int l[100000], r[100000], e[100000];

signed main() {
  int i, j;
  cin >> d;
  for (i = 0; i < d; i++)
    cin >> x[i];
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> l[i] >> r[i] >> e[i];
    l[i]--;
    r[i]--;
    int a = max(x[l[i]], x[r[i]]) + e[i];
    int b = min(x[l[i]], x[r[i]]) - e[i];

    int ans = r[i] - l[i] + 1;
    for (j = l[i]; j <= r[i]; j++) {
      if (b <= x[j] && x[j] <= a)
        ans--;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01781 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

const int MAXN = 5000005;

int X, Y, Z, a, b, c, n;
ll S[MAXN];
ll num[MAXN];
ll ans[MAXN];

inline ll get_S(int x) { return x < 0 ? 0 : S[x]; }

ll solve(int A, int B, int C, int K) {
  if (A < 0 || B < 0 || C < 0 || K < 0)
    return 0;
  return get_S(K) - get_S(K - A - 1) - get_S(K - B - 1) - get_S(K - C - 1) +
         get_S(K - A - B - 2) + get_S(K - A - C - 2) + get_S(K - B - C - 2) -
         get_S(K - A - B - C - 3);
}

int main() {
  S[0] = 1;
  for (int i = 1; i <= 5000000; i++)
    S[i] = S[i - 1] + i + 1;
  for (int i = 1; i <= 5000000; i++)
    S[i] += S[i - 1];

  scanf("%d %d %d %d %d %d", &X, &Y, &Z, &a, &b, &c);
  scanf("%d", &n);

  num[0] = 1;
  int p;
  for (int k = 1;; k++) {
    ll &sum = num[k];
    sum += solve(X - 1 - a, Y - 1 - b, Z - 1 - c, k);
    sum += solve(X - 1 - a, Y - 1 - b, c - 1, k - 1);
    sum += solve(X - 1 - a, b - 1, Z - 1 - c, k - 1);
    sum += solve(a - 1, Y - 1 - b, Z - 1 - c, k - 1);
    sum += solve(X - 1 - a, b - 1, c - 1, k - 2);
    sum += solve(a - 1, Y - 1 - b, c - 1, k - 2);
    sum += solve(a - 1, b - 1, Z - 1 - c, k - 2);
    sum += solve(a - 1, b - 1, c - 1, k - 3);

    if (sum <= num[k - 1]) {
      p = k - 1;
      break;
    }
  }
  ans[0]++;
  for (int i = 1; i <= p; i++)
    ans[i % n] += num[i] - num[i - 1];
  for (int i = 0; i < n; i++)
    printf("%lld%c", ans[i], " \\n"[i == n - 1]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int maxn = 1010;
long pat[maxn];

int main() {
  memset(pat, 0, sizeof(pat));
  int x, y, z, a, b, c, n;
  scanf("%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n);
  int i, j, k, dis;
  for (i = 0; i < x; i++)
    for (j = 0; j < y; j++)
      for (k = 0; k < z; k++) {
        dis = abs(a - i) + abs(b - j) + abs(c - k);
        pat[dis % n]++;
      }
  for (i = 0; i < n - 1; i++)
    printf("%d ", pat[i]);
  printf("%d", pat[n - 1]);
  printf("\\n");
  return 0;
}
Problem p01781 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

ll X, Y, Z, A, B, C, N;
ll cnt_x[1000], cnt_y[1000], cnt_z[1000], res[1000];

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

  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++) {
    int diff = abs(i - A) % N;
    cnt_x[diff]++;
  }
  for (int i = 0; i < Y; i++) {
    int diff = abs(i - B) % N;
    cnt_y[diff]++;
  }
  for (int i = 0; i < Z; i++) {
    int diff = abs(i - C) % N;
    cnt_z[diff]++;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        int idx = i + j + k;
        if (idx >= 2 * N)
          idx -= 2 * N;
        else if (idx >= N)
          idx -= N;
        res[idx] += cnt_x[i] * cnt_y[j] * cnt_z[k];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << res[i] << " \\n"[i == N - 1];
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

ll X, Y, Z, A, B, C, N;
ll cnt_x[1000], cnt_y[1000], cnt_z[1000], res[1000];

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

  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++) {
    int diff = abs(i - A) % N;
    cnt_x[diff]++;
  }
  for (int i = 0; i < Y; i++) {
    int diff = abs(i - B) % N;
    cnt_y[diff]++;
  }
  for (int i = 0; i < Z; i++) {
    int diff = abs(i - C) % N;
    cnt_z[diff]++;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        res[(i + j + k) % N] += cnt_x[i] * cnt_y[j] * cnt_z[k];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << res[i] << " \\n"[i == N - 1];
  }
}
Problem p01781 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int X, Y, Z, A, B, C, N, dx[1009], dy[1009], dz[1009];
long long ret[1009];
int main() {
  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++)
    dx[abs(A - i) % N]++;
  for (int i = 0; i < Y; i++)
    dy[abs(B - i) % N]++;
  for (int i = 0; i < Z; i++)
    dz[abs(C - i) % N]++;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int c = (i + j) % N;
      for (int k = 0; k < N; k++) {
        ret[c] += 1LL * dx[i] * dy[j] * dz[k];
        if (++c >= N)
          c = 0;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i)
      cout << ' ';
    cout << ret[i];
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int X, Y, Z, A, B, C, N, dx[1009], dy[1009], dz[1009];
long long ret[1009];
int main() {
  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++)
    dx[abs(A - i) % N]++;
  for (int i = 0; i < Y; i++)
    dy[abs(B - i) % N]++;
  for (int i = 0; i < Z; i++)
    dz[abs(C - i) % N]++;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        ret[(i + j + k) % N] += 1LL * dx[i] * dy[j] * dz[k];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i)
      cout << ' ';
    cout << ret[i];
  }
  cout << endl;
  return 0;
}