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

#include <cstring>
#include <iostream>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

void solve(int n) {
  if (n == 1) {
    cout << '#' << endl;
    return;
  }
  char field[n][n + 1];
  memset(field, ' ', sizeof(field));
  for (int i = 0; i < n; i++) {
    field[i][n] = '\\0';
  }
  int x = 0, y = n - 1, d = 0;
  while (true) {
    field[y][x] = '#';
    int newX = x + dx[d];
    int newY = y + dy[d];
    if (newX < 0 || n <= newX || newY < 0 || n <= newY ||
        field[newY + dy[d]][newX + dx[d]] == '#') {
      d = (d + 1) % 4;
      newX = x + dx[d];
      newY = y + dy[d];
    }
    int count = 0;
    for (int i = 0; i < 4; i++) {
      int cx = newX + dx[i];
      int cy = newY + dy[i];
      if (0 <= cx && cx < n && 0 <= cy && cy < n && field[cy][cx] == '#')
        count++;
    }
    if (count > 1)
      break;
    x = newX;
    y = newY;
  }
  for (int i = 0; i < n; i++) {
    cout << field[i] << endl;
  }
}

int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++) {
    int n;
    cin >> n;
    if (i != 0)
      cout << endl;
    solve(n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

void solve(int n) {
  char field[n][n + 1];
  memset(field, ' ', sizeof(field));
  for (int i = 0; i < n; i++) {
    field[i][n] = '\\0';
  }
  int x = 0, y = n - 1, d = 0;
  while (true) {
    field[y][x] = '#';
    int newX = x + dx[d];
    int newY = y + dy[d];
    if (newX < 0 || n <= newX || newY < 0 || n <= newY ||
        field[newY + dy[d]][newX + dx[d]] == '#') {
      d = (d + 1) % 4;
      newX = x + dx[d];
      newY = y + dy[d];
    }
    int count = 0;
    for (int i = 0; i < 4; i++) {
      int cx = newX + dx[i];
      int cy = newY + dy[i];
      if (0 <= cx && cx < n && 0 <= cy && cy < n && field[cy][cx] == '#')
        count++;
    }
    if (count > 1)
      break;
    x = newX;
    y = newY;
  }
  for (int i = 0; i < n; i++) {
    cout << field[i] << endl;
  }
}

int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++) {
    int n;
    cin >> n;
    solve(n);
  }
  return 0;
}
Problem p00141 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

#define MAX_N 300

int x[MAX_N][MAX_N];
int n, a, cx, cy, cnt;

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    if (i >= 1) {
      cout << endl;
    }
    cnt = 0;
    cin >> a;
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k < MAX_N; k++) {
        x[j][k] = 0;
      }
    }
    cx = 0;
    cy = a - 1;
    x[cy][cx] = 1;
    while (cnt < a) {
      while (true) {
        int Y1 = cy + dy[cnt % 4];
        int X1 = cx + dx[cnt % 4];
        int Y2 = cy + dy[cnt % 4] * 2;
        int X2 = cx + dx[cnt % 4] * 2;
        if (Y1 >= 0 && Y1 < a && X1 >= 0 && X1 < a) {
          if (Y2 >= 0 && Y2 < a && X2 >= 0 && X2 < a) {
            if (x[Y2][X2] == 1) {
              break;
            }
          }
        } else {
          break;
        }
        cx = X1;
        cy = Y1;
        x[cy][cx] = 1;
      }
      cnt++;
    }
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < a; k++) {
        if (x[j][k] == 1) {
          cout << '#';
        } else {
          cout << ' ';
        }
      }
      cout << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 7500
int n, cnt, cx, cy;
bool x[MAX_N][MAX_N];
bool y[MAX_N][MAX_N];
int main() {
  while (cin >> n) {
    cnt = 0;
    memset(x, true, sizeof(x));
    memset(y, false, sizeof(y));
    for (int i = 10; i < 10 + n; i++) {
      for (int j = 10; j < 10 + n; j++) {
        x[i][j] = false;
      }
    }
    cy = 10 + n;
    cx = 10;
    while (cnt <= MAX_N) {
      while (x[cy - 1][cx] == false && y[cy - 2][cx] == false &&
             y[cy - 1][cx + 1] == false) {
        cy--;
        y[cy][cx] = true;
      }
      while (x[cy][cx + 1] == false && y[cy][cx + 2] == false) {
        cx++;
        y[cy][cx] = true;
      }
      while (x[cy + 1][cx] == false && y[cy + 2][cx] == false &&
             y[cy + 1][cx - 1] == false) {
        cy++;
        y[cy][cx] = true;
      }
      while (x[cy][cx - 1] == false && y[cy][cx - 2] == false) {
        cx--;
        y[cy][cx] = true;
      }
      cnt++;
    }
    for (int i = 10; i < 10 + n; i++) {
      for (int j = 10; j < 10 + n; j++) {
        if (y[i][j] == true) {
          cout << '#';
        } else {
          cout << ' ';
        }
      }
      cout << endl;
    }
  }
}
Problem p00141 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

void func() {
  char table[110][110];

  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      if (i >= 100 - N + 1 && i <= 100 && j >= 1 && j <= N)
        table[i][j] = '#';
      else
        table[i][j] = ' ';
    }
  }

  int x = 1;
  int y = 100;
  int k = 0;
  bool f = true;

  while (1) {
    if (table[y + dy[k % 4]][x + dx[k % 4]] == ' ')
      break;
    bool b = false;
    while (table[y + dy[k % 4]][x + dx[k % 4]] != ' ') {
      if (f || b)
        table[y + dy[(k + 1) % 4]][x + dx[(k + 1) % 4]] = ' ';
      if (!b)
        b = true;
      if (f)
        f = false;
      x += dx[k % 4];
      y += dy[k % 4];
    }
    k++;
  }

  for (int i = 100 - N + 1; i <= 100; i++) {
    for (int j = 1; j <= N; j++) {
      cout << table[i][j];
    }
    cout << endl;
  }
}

int main() {

  cin >> d;
  while (d--) {
    cin >> N;
    func();
    if (d)
      cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

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

void func() {
  char table[102][102];

  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      if (i >= 100 - N && i <= 100 && j >= 1 && j <= N)
        table[i][j] = '#';
      else
        table[i][j] = ' ';
    }
  }

  int x = 1;
  int y = 100;
  int k = 0;
  bool f = true;

  while (1) {
    if (table[y + dy[k % 4]][x + dx[k % 4]] == ' ')
      break;
    bool b = false;
    while (table[y + dy[k % 4]][x + dx[k % 4]] != ' ') {
      if (f || b)
        table[y + dy[(k + 1) % 4]][x + dx[(k + 1) % 4]] = ' ';
      if (!b)
        b = true;
      if (f)
        f = false;
      x += dx[k % 4];
      y += dy[k % 4];
    }
    k++;
  }

  for (int i = 100 - N; i <= 100; i++) {
    for (int j = 1; j <= N; j++) {
      cout << table[i][j];
    }
    cout << endl;
  }
}

int main() {

  int d;

  cin >> d;
  while (d--) {
    cin >> N;
    func();
    cout << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

#define EPS 1.0e-10
#define rep(idx, max) for (int idx = 0, idx##Max = max; idx < idx##Max; idx++)
#define rrep(idx, min, max)                                                    \\
  for (int idx = min, idx##Max = max; idx < idx##Max; idx++)
#define erep(idx, min, max)                                                    \\
  for (int idx = min, idx##Max = max; idx <= idx##Max; idx++)

#define rev(idx, max) for (int idx = max - 1; idx >= 0; idx--)
#define MAX_N 128
char g_map[MAX_N][MAX_N];

int main() {
  static int dx[] = {-1, 0, 1, 0};
  static int dy[] = {0, -1, 0, 1};
  int dir;
  int edgeLen;
  int testCases;

  std::cin >> testCases;
  while (testCases-- > 0) {
    int x, y;
    std::cin >> edgeLen;
    ::memset(g_map, ' ', sizeof(g_map));

    rep(y, edgeLen) { g_map[y][edgeLen] = '\\0'; }

    dir = 0;
    // ????????????????????????????????????????????????2????????????
    x = edgeLen + 2 - 1;
    y = edgeLen + 2 - 1;
    for (int n = edgeLen + 2; n >= 2;) {
      rep(i, n == 2 ? 1 : 2) {
        rep(j, n) {
          g_map[y][x] = '#';
          //::printf("(%d, %d)\\n", x, y);
          x += dx[dir];
          y += dy[dir];
        }
        x -= dx[dir];
        y -= dy[dir];
        dir = (dir + 1) & 0x03;
      }
      n -= 2;
    }

    rep(y, edgeLen) { ::puts(&g_map[y][0]); }
    if (testCases != 0) {
      ::puts("");
    }
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

#define EPS 1.0e-10
#define rep(idx, max) for (int idx = 0, idx##Max = max; idx < idx##Max; idx++)
#define rrep(idx, min, max)                                                    \\
  for (int idx = min, idx##Max = max; idx < idx##Max; idx++)
#define erep(idx, min, max)                                                    \\
  for (int idx = min, idx##Max = max; idx <= idx##Max; idx++)

#define rev(idx, max) for (int idx = max - 1; idx >= 0; idx--)
#define MAX_N 128
char g_map[MAX_N][MAX_N];

int main() {
  //	static int dx[] = { 0, 1, 0, -1 };
  //	static int dy[] = { -1, 0, 1, 0 };
  static int dx[] = {-1, 0, 1, 0};
  static int dy[] = {0, -1, 0, 1};
  int dir;
  int edgeLen;
  int testCases;

  std::cin >> testCases;
  while (testCases) {
    int x, y;
    std::cin >> edgeLen;
    ::memset(g_map, ' ', sizeof(g_map));

    rep(y, edgeLen) { g_map[y][edgeLen] = '\\0'; }

    dir = 0;
    x = edgeLen + 2 - 1;
    y = edgeLen + 2 - 1;
    for (int n = edgeLen + 2; n >= 2;) {
      rep(i, n == 2 ? 1 : 2) {
        rep(j, n) {
          g_map[y][x] = '#';
          //::printf("(%d, %d)\\n", x, y);
          x += dx[dir];
          y += dy[dir];
        }
        x -= dx[dir];
        y -= dy[dir];
        dir = (dir + 1) & 0x03;
      }
      n -= 2;
    }

    rep(y, edgeLen) {
      ::puts(&g_map[y][0]);
      // rep(x, edgeLen)	{	::putchar(g_map[y][x]);	}
      // ::puts("");
    }
    ::puts("");
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    bool isAppeared[10001];
    int data[10001];
    fill(isAppeared, isAppeared + 10001, false);
    fill(data, data + 10001, 0);
    // memset(isAppeared,0,sizeof(isAppeared));
    // memset(data,0,sizeof(data));
    for (int i = 1; i < n; i++) {
      int r = (i * i) % n;
      isAppeared[r] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (isAppeared[i] && isAppeared[j]) {
          int ret = i - j;
          if (ret < 0)
            ret += n;
          if (((n - 1) / 2) < ret) {
            ret = n - ret;
          }
          data[ret]++;
        }
      }
    }
    for (int i = 1; i <= ((n - 1) / 2); i++) {
      if (data[i] != 0)
        cout << data[i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    bool isAppeared[10001];
    int data[10001];
    fill(isAppeared, isAppeared + 10001, false);
    fill(data, data + 10001, 0);
    // memset(isAppeared,0,sizeof(isAppeared));
    // memset(data,0,sizeof(data));
    for (int i = 1; i < n; i++) {
      int r = (i * i) % n;
      isAppeared[r] = true;
    }
    for (int i = 0; i < 10001; i++) {
      for (int j = 0; j < 10001; j++) {
        if (isAppeared[i] && isAppeared[j]) {
          int ret = i - j;
          if (ret < 0)
            ret += n;
          if (((n - 1) / 2) < ret) {
            ret = n - ret;
          }
          data[ret]++;
        }
      }
    }
    for (int i = 1; i <= ((n - 1) / 2); i++) {
      if (data[i] != 0)
        cout << data[i] << endl;
    }
  }

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

#include <algorithm>
#include <iostream>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
int number[5000];

void solve() {
  int n;
  while (cin >> n, n) {
    memset(number, 0, sizeof(number));
    set<int> Set;
    for (int i = 1; i < n; ++i) {
      Set.insert((i * i) % n);
    }
    vector<int> num;
    int size = (n - 1) / 2;
    set<int>::iterator it = Set.begin();
    while (it != Set.end()) {
      set<int>::iterator it2 = Set.begin();
      while (it2 != Set.end()) {
        int temp = *it2 - *it;
        if (temp < 0) {
          temp += n;
        }
        if (temp > size) {
          temp = n - temp;
        }
        ++number[temp];
        ++it2;
      }
      ++it;
    }
    for (int i = 1; i <= size; ++i) {
      cout << number[i] << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

void solve() {
  int n;
  while (cin >> n, n) {
    set<int> Set;
    for (int i = 1; i < n; ++i) {
      Set.insert((i * i) % n);
    }
    vector<int> num;
    set<int>::iterator it = Set.begin();
    while (it != Set.end()) {
      num.push_back(*it);
      ++it;
    }
    vector<int> diff;
    for (int i = 0; i < num.size(); ++i) {
      for (int j = 0; j < num.size(); ++j) {
        if (i == j) {
          continue;
        }
        int temp = num[i] - num[j];
        if (temp < 0) {
          temp += n;
        }
        if (temp > (n - 1) / 2) {
          temp = n - temp;
        }
        diff.push_back(temp);
      }
    }
    sort(diff.begin(), diff.end());
    int now = diff[0];
    int count = 1;
    for (int i = 1; i < diff.size(); ++i) {
      if (diff[i] != now) {
        cout << count << endl;
        now = diff[i];
        count = 1;
      } else {
        ++count;
      }
    }
    if (count >= 2) {
      cout << count << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define INF 1e+8
#define EPS 1e-9
#define PB push_back
#define fi first
#define se second
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef long long ll;
typedef pair<int, int> Pii;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    bool flg[12800] = {0};
    vector<int> numbers;
    reps(i, 1, n / 2 + 1) {
      int tmp = (i * i) % n;
      if (flg[tmp]++)
        continue;
      numbers.PB(tmp);
    }
    int cnt[12800] = {0};
    int len = numbers.size();
    sort(numbers.begin(), numbers.end());
    rep(i, len) {
      reps(j, i + 1, len) {
        int tmp = numbers[j] - numbers[i];
        if (tmp > (n - 1) / 2)
          tmp = n - tmp;
        cnt[tmp]++;
      }
    }
    reps(i, 1, (n - 1) / 2 + 1) { printf("%d\\n", 2 * cnt[i]); }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define INF 1e+8
#define EPS 1e-9
#define PB push_back
#define fi first
#define se second
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef long long ll;
typedef pair<int, int> Pii;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    bool flg[12800] = {0};
    vector<int> numbers;
    map<int, int> cnt;
    reps(i, 1, n / 2 + 1) {
      int tmp = (i * i) % n;
      if (flg[tmp]++)
        continue;
      numbers.PB(tmp);
    }
    int len = numbers.size();
    sort(numbers.begin(), numbers.end());
    rep(i, len) {
      reps(j, i + 1, len) {
        int tmp = numbers[j] - numbers[i];
        if (tmp > (n - 1) / 2)
          tmp = n - tmp;
        cnt[tmp]++;
      }
    }
    map<int, int>::iterator it;
    for (it = cnt.begin(); it != cnt.end(); it++) {
      cout << 2 * it->se << endl;
    }
  }
  return 0;
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int main(void) {
  int n;
  set<int> target;
  int data[10000];
  int change[10000];

  while (cin >> n && n != 0) {
    target.clear();
    for (int i = 0; i < 10000; i++)
      change[i] = data[i] = 0;
    for (int i = 1; i < n; i++) {
      target.insert(i * i % n);
    }

    copy(target.begin(), target.end(), change);

    int piyo = target.size();
    int hoge = (n - 1) / 2;
    for (int i = 0; i < piyo; i++) {
      for (int j = 0; j < piyo; j++) {
        int answer = change[i] - change[j];
        if (answer == 0)
          continue;
        while (1) {
          if (answer < 0) {
            answer += n;
            continue;
          } else if (answer > hoge) {
            answer = n - answer;
            continue;
          }
          data[answer]++;
          break;
        }
      }
    }

    for (int i = 1; i <= hoge; i++) {
      cout << data[i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
  int n;
  set<int> target;
  int data[10000];
  int change[20000];

  while (cin >> n && n != 0) {
    target.clear();
    for (int i = 0; i < 10000; i++)
      data[i] = 0;
    for (int i = 0; i < 20000; i++)
      change[i] = 0;
    for (int i = 1; i < n; i++) {
      target.insert(i * i % n);
    }

    for (set<int>::iterator it = target.begin(); it != target.end(); it++) {
      for (set<int>::iterator it2 = target.begin(); it2 != target.end();
           it2++) {
        int answer = *it - *it2;
        if (change[answer + 10000] != 0) {
          data[change[answer + 10000]]++;
          continue;
        }
        while (1) {
          if (answer < 0) {
            answer += n;
            continue;
          } else if (answer > (n - 1) / 2) {
            answer = n - answer;
            continue;
          }
          data[answer]++;
          break;
        }
      }
    }

    for (int i = 1; i <= (n - 1) / 2; i++) {
      cout << data[i] << endl;
    }
  }

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

#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
const int N = 1e4 + 10;
#define PB push_back
int abs(int n) { return n > 0 ? n : -n; }
int main() {
  int n, a[N], mid, temp;
  set<int> s;
  vector<int> v;
  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      return 0;
    s.clear();
    v.clear();
    for (int i = 1; i < n; i++)
      s.insert((i * i) % n);
    mid = n >> 1;
    for (int i = 1; i <= mid; i++)
      a[i] = 0;
    for (int i : s)
      v.PB(i);
    for (int i = 0; i < v.size(); i++)
      for (int j = 0; j < v.size(); j++)
        if (i != j) {
          temp = abs(v[j] - v[i]);
          if (temp > mid)
            a[n - temp]++;
          else
            a[temp]++;
        }
    for (int i = 1; i <= mid; i++)
      printf("%d\\n", a[i]);
  }
}


Error: -11

Fixed submission:

#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
const int N = 1e4 + 10;
#define PB push_back
int main() {
  int n, a[N], mid;
  set<int> s;
  vector<int> v;
  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      return 0;
    s.clear();
    v.clear();
    for (int i = 1; i < n; i++)
      s.insert((i * i) % n);
    mid = n >> 1;
    for (int i = 1; i <= mid; i++)
      a[i] = 0;
    for (int i : s)
      v.PB(i);
    for (int i = 0; i < v.size(); i++)
      for (int j = 0; j < v.size(); j++)
        if (i != j) {
          if (v[j] - v[i] > mid)
            a[n - (v[j] - v[i])]++;
          else
            a[v[j] - v[i]]++;
        }
    for (int i = 1; i <= mid; i++)
      printf("%d\\n", a[i] * 2);
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    set<int> remains;
    for (int i = 1; i < n; i++) {
      int remain = (i * i) % n;
      remains.insert(remain);
    }
    int max = (n - 1) / 2;
    int *differences = new int[max + 1];
    fill_n(differences, max + 1, 0);
    for (set<int>::iterator it1 = remains.begin(); it1 != remains.end();
         it1++) {
      for (set<int>::iterator it2 = it1; it2 != remains.end(); it2++) {
        if (it1 == it2) {
          continue;
        }
        int diff = *it1 - *it2;
        if (diff < 0) {
          diff += n;
        }
        if (diff > max) {
          diff = n - diff;
        }
        differences[diff] += 2;
      }
    }
    for (int i = 1; i <= max; i++) {
      cout << differences[i] << endl;
    }
    delete[] differences;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    set<int> remains;
    for (int i = 1; i < n; i++) {
      int remain = (i * i) % n;
      remains.insert(remain);
    }
    int max = (n - 1) / 2;
    int *differences = new int[max + 1];
    fill_n(differences, max + 1, 0);
    for (set<int>::iterator it1 = remains.begin(); it1 != remains.end();
         it1++) {
      for (set<int>::iterator it2 = remains.begin(); it2 != remains.end();
           it2++) {
        if (it1 == it2) {
          continue;
        }
        int diff = *it1 - *it2;
        if (diff < 0) {
          diff += n;
        }
        if (diff > max) {
          diff = n - diff;
        }
        differences[diff]++;
      }
    }
    for (int i = 1; i <= max; i++) {
      cout << differences[i] << endl;
    }
    delete[] differences;
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define N_MAX 10000
int main() {

  int n, fact[N_MAX + 1];
  bool prime[N_MAX + 1] = {};
  set<int> digit;

  int mul[N_MAX * 1];

  for (int i = 1; i <= N_MAX; i++) {
    fact[i] = i * i;
  }

  while (cin >> n, n) {
    fill_n(mul, n + 1, 0);

    for (int i = 1; i < n; i++) {
      digit.insert(fact[i] % n);
    }
    for (__typeof digit.begin() it = digit.begin(); it != digit.end(); it++) {
      for (__typeof digit.begin() iitt = digit.begin(); iitt != digit.end();
           iitt++) {
        if (it == iitt)
          continue;

        int calc = *it - *iitt;
        if (calc < 0)
          calc += n;
        if ((n - 1) / 2 < calc)
          calc = n - calc;
        mul[calc]++;
      }
    }

    for (int i = 1; i <= (n - 1) / 2; i++) {
      printf("%d\\n", mul[i]);
    }
    digit.clear();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N_MAX 10000
int main() {

  int n, fact[N_MAX + 1];
  set<int> digit;
  map<int, int> mul;

  for (int i = 1; i <= N_MAX; i++) {
    fact[i] = i * i;
  }

  while (cin >> n, n) {
    for (int i = 1; i < n; i++) {
      digit.insert(fact[i] % n);
    }
    for (__typeof digit.begin() it = digit.begin(); it != digit.end(); it++) {
      for (__typeof digit.begin() iitt = digit.begin(); iitt != digit.end();
           iitt++) {
        if (it == iitt)
          continue;

        int calc = *it - *iitt;
        if (calc < 0)
          calc += n;
        if ((n - 1) / 2 < calc)
          calc = n - calc;
        mul[calc]++;
      }
    }

    for (int i = 1; i <= (n - 1) / 2; i++) {
      printf("%d\\n", mul[i]);
    }

    mul.clear();
    digit.clear();
  }
}