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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reep(i, f, t) for (int i = f; i < int(t); ++i)
#define rep(i, n) reep(i, 0, n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    vi nums;
    nums.reserve(10000);
    reep(i, 1, n) nums.push_back((i * i) % n);
    int half = (n - 1) / 2;

    sort(nums.begin(), nums.end());
    int size = distance(nums.begin(), unique(nums.begin(), nums.end()));

    vi ans(half + 1, 0);
    rep(i, size) {
      rep(j, i + 1) {
        int p = abs(nums[i] - nums[j]);
        if (p > half)
          p = n - p;
        ++ans[p];
      }
    }
    reep(i, 1, half + 1) printf("%d\\n", ans[i] * 2);
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reep(i, f, t) for (int i = f; i < int(t); ++i)
#define rep(i, n) reep(i, 0, n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  int n;
  vi nums;
  nums.reserve(10000);

  while (scanf("%d", &n), n) {
    reep(i, 1, n) nums.push_back((i * i) % n);
    int half = (n - 1) / 2;

    sort(nums.begin(), nums.end());
    int size = distance(nums.begin(), unique(nums.begin(), nums.end()));

    vi ans(half + 1, 0);
    rep(i, size) {
      rep(j, i + 1) {
        int p = abs(nums[i] - nums[j]);
        if (p > half)
          p = n - p;
        ++ans[p];
      }
    }
    reep(i, 1, half + 1) printf("%d\\n", ans[i] * 2);
  }

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

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

using namespace std;

const int MAX = 10000;
const int INF = (1 << 20);

int data[MAX], n;

void solve() {
  vector<int> V;
  fill(data, data + MAX, 0);
  for (int i = 1; i < n; i++) {
    V.push_back((i * i) % n);
  }
  V.push_back(-1);
  V.push_back(INF);
  sort(V.begin(), V.end());
  vector<int> VV;
  int now = -1;
  while (1) {
    now = *upper_bound(V.begin(), V.end(), now);
    if (now == INF)
      break;
    VV.push_back(now);
  }
  for (int i = 0; i < VV.size(); i++)
    for (int j = 0; j < VV.size(); j++) {
      // if(VV[i]!=VV[j]) {
      int num = VV[i] - VV[j];
      if (num < 0)
        num += n;
      if (num > (n - 1) / 2)
        num = n - num;
      data[num]++;
      //}
    }
}

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

int main() {
  while (cin >> n && n) {
    solve();
    print();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int MAX = 10000;
int data[MAX], n;

void solve() {
  vector<int> V;
  fill(data, data + MAX, 0);
  for (int i = 1; i < n; i++) {
    sort(V.begin(), V.end());
    if (!(binary_search(V.begin(), V.end(), (i * i) % n)))
      V.push_back((i * i) % n);
  }

  for (int i = 0; i < V.size(); i++)
    for (int j = 0; j < V.size(); j++)
      if (i != j) {
        int num = V[i] - V[j];
        if (num < 0)
          num += n;
        if (num > (n - 1) / 2)
          num = n - num;
        data[num]++;
      }
}

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

int main() {
  while (cin >> n && n) {
    solve();
    print();
  }
  return 0;
}
Problem p00142 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 10010;

int main(void) {
  int square[MAX];
  for (int i = 1; i < MAX + 1; i++)
    square[i - 1] = i * i;

  while (true) {
    int n, size;
    int raw_mod[MAX], mod[MAX], hist[MAX] = {};

    cin >> n;
    if (n == 0)
      break;
    int m = (n - 1) / 2;

    for (int i = 0; i < n - 1; i++)
      raw_mod[i] = square[i] % n;
    sort(raw_mod, raw_mod + n - 1);

    mod[0] = raw_mod[0];
    for (int i = 1, j = 1; i < n - 1; i++) {
      if (mod[j - 1] != raw_mod[i]) {
        mod[j] = raw_mod[i];
        j++;
      }
      size = j;
    }

    for (int i = 0; i < size - 1; i++)
      for (int j = i + 1; j < size; j++) {

        int tmp1 = mod[i] - mod[j];
        if (tmp1 < 0)
          tmp1 += n;
        if (tmp1 > m)
          tmp1 = n - tmp1;

        int tmp2 = mod[j] - mod[i];
        if (tmp2 < 0)
          tmp2 += n;
        if (tmp2 > m)
          tmp2 = n - tmp2;

        hist[tmp1 - 1] += 1;
        hist[tmp2 - 1] += 1;
      }

    for (int i = 0; i < m; i++)
      cout << hist[i] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 10010;

int main(void) {
  int square[MAX];
  for (int i = 1; i < MAX + 1; i++)
    square[i - 1] = i * i;

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int m = (n - 1) / 2;

    int raw_mod[n - 1], mod[n - 1];
    for (int i = 0; i < n - 1; i++)
      raw_mod[i] = square[i] % n;

    sort(raw_mod, raw_mod + n - 1);
    mod[0] = raw_mod[0];
    int size;
    for (int i = 1, j = 1; i < n - 1; i++) {
      if (mod[j - 1] != raw_mod[i]) {
        mod[j] = raw_mod[i];
        j++;
      }
      size = j;
    }

    int hist[MAX] = {};
    for (int i = 0; i < size - 1; i++)
      for (int j = i + 1; j < size; j++) {

        int tmp1 = mod[i] - mod[j];
        if (tmp1 < 0)
          tmp1 += n;
        if (tmp1 > m)
          tmp1 = n - tmp1;

        int tmp2 = mod[j] - mod[i];
        if (tmp2 < 0)
          tmp2 += n;
        if (tmp2 > m)
          tmp2 = n - tmp2;

        hist[tmp1 - 1] += 1;
        hist[tmp2 - 1] += 1;
      }

    for (int i = 0; i < size; i++)
      cout << hist[i] << endl;
  }
  return 0;
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
vector<int> v;
int n;
int cnt[10010];
int main() {
  while (1) {
    memset(cnt, 0, sizeof(cnt));
    v.clear();
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 1; i < n; i++)
      v.pb((i * i) % n);
    SORT(v);
    v.erase(unique(all(v)), v.end());
    // cout << v.size() << endl;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        if (i == j)
          continue;
        int k = v[i] - v[j];
        if (k < 0)
          k += n;
        if (k > (n - 1) / 2)
          k = n - k;
        cnt[k]++;
      }
    }
    for (int i = 1; i <= (n - 1) / 2; i++)
      printf("%d\\n", cnt[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int n;
int dist(int a, int b) {
  int t = a - b;
  if (t < 0)
    t += n;
  if (t > (n - 1) / 2)
    t = n - t;
  return t;
}
int main() {
  while (1) {
    set<int> s;
    map<int, int> m;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i < n; i++) {
      int k = i * i;
      s.insert(k % n);
    }
    set<int>::iterator it;
    vector<int> v;
    for (it = s.begin(); it != s.end(); it++)
      v.pb(*it);
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        if (i == j)
          continue;
        int d = dist(v[i], v[j]);
        m[d]++;
      }
    }
    for (int i = 1; i <= (n - 1) / 2; i++)
      cout << m[i] << endl;
  }
  return 0;
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  while (cin >> n, n) {
    set<int> se;

    REP(i, n - 1) { se.insert((i + 1) * (i + 1) % n); }
    vector<int> v((n - 1) / 2);
    FOR(it1, se) {
      for (set<int>::iterator it2 = it1; it2 != se.end(); it2++) {
        if (*it1 == *it2)
          continue;
        int temp = abs(*it1 - *it2);
        if (temp > (n - 1) / 2)
          temp = n - temp;
        v[temp - 1]++;
      }
    }

    FOR(it, v) { cout << *it * 2 << endl; }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  while (cin >> n, n) {
    set<int> se;

    REP(i, n - 1) { se.insert((i + 1) * (i + 1) % n); }
    vector<int> v((n - 1) / 2);
    FOR(it1, se) {
      FOR(it2, se) {
        if (*it1 == *it2)
          continue;
        int temp = abs(*it1 - *it2);
        if (temp > (n - 1) / 2)
          temp = n - temp;
        v[temp - 1]++;
      }
    }

    FOR(it, v) { cout << *it << endl; }
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, delta;
  while (cin >> n, n) {
    vector<int> table(n, -1);
    int t[5001] = {0};
    for (int i = 1; i < n; i++) {
      table[i] = (i * i) % n;
    }
    sort(table.begin(), table.end());
    table.erase(unique(table.begin(), table.end()), table.end());

    for (int i = 1; i < table.size(); i++) {
      for (int j = 1; j < table.size(); j++) {
        delta = table[i] - table[j];
        if (delta < 0)
          delta += n;
        if (delta > (n - 1) / 2)
          delta = n - delta;
        t[delta]++;
      }
    }
    for (int i = 1; i <= (n - 1) / 2; i++) {
      cout << t[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    set<int> table;
    int t[5001] = {0};
    for (int i = 1; i < n; i++) {
      table.insert((i * i) % n);
    }
    for (set<int>::iterator it = table.begin(); it != table.end(); ++it) {
      for (set<int>::iterator it2 = table.begin(); it2 != table.end(); ++it2) {
        int delta = (*it) - (*it2);
        if (delta < 0)
          delta += n;
        if (delta > (n - 1) / 2)
          delta = n - delta;
        t[delta]++;
      }
    }
    for (int i = 1; i <= (n - 1) / 2; i++) {
      cout << t[i] << endl;
    }
  }
}
Problem p00142 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
static inline lint inl() {
  lint x;
  scanf("%lld", &x);
  return x;
}
template <typename T> void show(T &a, char del = '\\n', char last = '\\n') {
  rep(i, a.size() - 1) cout << a[i] << del;
  cout << a[a.size() - 1] << last;
}

int main() {
  int n;
  while (n = in()) {
    vector<int> v;
    vector<int> counter(n + 1, 0);
    lep(i, n - 1) v.push_back(i * i % n);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    rep(i, v.size()) {
      rep(j, v.size()) {
        if (i == j)
          continue;
        int x = v[i] - v[j];
        if (x < 0)
          x += n;
        if (x > (n - 1) / 2)
          x = n - x;
        counter[x]++;
      }
    }
    lep(i, (n - 1) / 2) { printf("%d\\n", counter[i]); }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
static inline lint inl() {
  lint x;
  scanf("%lld", &x);
  return x;
}
template <typename T> void show(T &a, char del = '\\n', char last = '\\n') {
  rep(i, a.size() - 1) cout << a[i] << del;
  cout << a[a.size() - 1] << last;
}

int main() {
  int n;
  while (n = in()) {
    vector<int> v;
    vector<int> counter((n - 1) / 2, 0);
    lep(i, n - 1) v.push_back(i * i % n);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    rep(i, v.size()) {
      rep(j, v.size()) {
        if (i == j)
          continue;
        int x = v[i] - v[j];
        if (x < 0)
          x += n;
        if (x > (n - 1) / 2)
          x = n - x;
        counter[x - 1]++;
      }
    }
    show(counter);
  }
  return 0;
}
Problem p00142 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

int n;

set<int> si;
set<int>::iterator it, it2;

int main() {
  while (cin >> n && n) {
    int cnt[10000] = {};
    FOR(i, 1, n) { si.insert((i * i) % n); }
    for (it = si.begin(); it != si.end(); it++) {
      for (it2 = si.begin(); it2 != si.end(); it2++) {
        if (it == it2)
          continue;
        int num = *it - *it2;
        if (num < 0)
          num += n;
        if (num > (n - 1) / 2)
          num = n - num;
        cnt[num]++;
      }
    }
    FOR(i, 1, (n - 1) / 2 + 1) { cout << cnt[i] << endl; }
    si.clear();
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

// MLE
int n;
vector<int> v1, v2;

int main() {
  while (cin >> n && n) {
    ll co[1000] = {};
    FOR(i, 1, n) {
      int k = i * i % n;
      v1.push_back(k);
    }
    sort(ALL(v1));
    v1.erase(unique(ALL(v1)), v1.end());
    // REP(i, v1.size())cout << v1[i] << " ";
    REP(i, v1.size()) {
      REP(j, v1.size()) {
        if (i != j) {
          int k = v1[i] - v1[j];
          if (k < 0)
            k += n;
          if (k > (n - 1) / 2)
            k = n - k;
          v2.push_back(k);
        }
      }
    }
    REP(i, v2.size()) { co[v2[i]]++; }
    FOR(i, 1, (n - 1) / 2 + 1) { cout << co[i] << endl; }
    v1.clear();
    v2.clear();
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int data[20000];

int f(int tmp, int n) {
  int pre = tmp;
  while (1) {
    bool f = true;
    if (tmp < 0)
      tmp += n, f = false;
    if (tmp > (n - 1) / 2)
      tmp = n - tmp, f = false;
    if (f)
      break;
  }
  data[10000 + pre] = tmp;
  return tmp;
}

int main() {
  int n;
  while (cin >> n && n) {
    rep(i, 20000) data[i] = -1;
    set<int> a;
    for (int i = 1; i < n; i++) {
      a.insert((i * i) % n);
    }

    vector<int> vec;
    for (int x : a)
      vec.pb(x);
    int b[10000] = {};

    rep(i, vec.size()) {
      rep(j, vec.size()) {
        if (i == j)
          continue;
        int tmp = vec[i] - vec[j];
        if (data[10000 + tmp] != -1)
          tmp = data[10000 + tmp];
        else
          tmp = f(tmp, n);
        b[tmp]++;
      }
    }
    int num = (n - 1) / 2;
    for (int i = 1; i <= num; i++) {
      printf("%d\\n", b[i]);
    }
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int data[20000];

int f(int tmp, int n) {
  if (data[10000 + tmp] != -1)
    return data[10000 + tmp];
  int pre = tmp;
  while (1) {
    bool f = true;
    if (tmp < 0)
      tmp += n, f = false;
    if (tmp > (n - 1) / 2)
      tmp = n - tmp, f = false;
    if (f)
      break;
  }
  data[10000 + pre] = tmp;
  return tmp;
}

int main() {
  int n;
  while (cin >> n && n) {
    rep(i, 20000) data[i] = -1;
    set<int> a;
    for (int i = 1; i < n; i++) {
      a.insert((i * i) % n);
    }

    vector<int> vec;
    for (int x : a)
      vec.pb(x);
    map<int, int> b;

    rep(i, vec.size()) {
      rep(j, vec.size()) {
        if (i == j)
          continue;
        int tmp = vec[i] - vec[j];
        tmp = f(tmp, n);
        b[tmp]++;
      }
    }
    for (pii x : b) {
      printf("%d\\n", x.second);
    }
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool flg[10002];
int cnt[10002];

int main() {
  int n;

  while (cin >> n, n) {
    memset(flg, 0, sizeof(flg));
    memset(cnt, 0, sizeof(cnt));

    for (int i = 1; i < n; i++) {
      flg[(i * i) % n] = true;
    }

    for (int i = 0; i < n; i++) {
      if (!flg[i])
        continue;

      for (int j = i + 1; j < n; j++) {
        if (!flg[j])
          continue;

        int cal = j - i;
        if (cal > (n - 1) / 2)
          cal = n - cal;
        cnt[cal]++;

        cal = i - j + n;
        if (cal > (n - 1) / 2)
          cal = n - cal;
        cnt[cal]++;
      }
    }

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

Error: TLE

Fixed submission:

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

int main(void) {
  int n;

  while (cin >> n && n) {
    /* ツ妥ヲツ暗ェツ段ツ階ツづ個計ツ算 */
    set<int> first;
    for (int i = 1; i < n; i++) {
      first.insert(i * i % n);
    }

    /* ツ妥ヲツ禿ア,ツ三ツ段ツ階ツづ個計ツ算 */
    int max = (n - 1) / 2;
    int count[max + 1];
    memset(count, 0, sizeof(count));
    for (set<int>::iterator i = first.begin(); i != first.end(); i++) {
      for (set<int>::iterator j = first.begin(); j != first.end(); j++) {
        if (*i == *j)
          continue;

        int diff = *i - *j;
        if (diff <= 0)
          diff += n;
        if (diff > max)
          diff = n - diff;
        count[diff]++;
      }
    }

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

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

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n;

  while (cin >> n, n) {
    set<int> s;
    vector<int> v0;
    vector<int> v1;

    v1.resize((n + 1) / 2);
    for (int i = 0; i < v1.size(); i++)
      v1[i] = 0;

    for (int i = 1; i < n; i++) {
      s.insert(i * i % n);
    }
    for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++) {
      v0.push_back(*iter);
    }
    for (int i = 0; i < v0.size(); i++) {
      for (int j = 0; j < v0.size(); j++) {
        if (i != j) {
          int tmp = (v0[i] - v0[j] + n) % n;
          if (tmp > (n - 1) / 2) {
            tmp = n - tmp;
          }
          v1[tmp]++;
        }
      }
    }
    for (int i = 1; i < (n + 1) / 2; i++)
      cout << v1[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n;

  while (cin >> n, n) {
    set<int> s;
    map<int, int> m;
    vector<int> v;

    for (int i = 1; i < n; i++) {
      s.insert(i * i % n);
    }
    for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++) {
      v.push_back(*iter);
    }
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        if (i != j) {
          int tmp = (v[i] - v[j] + n) % n;
          if (tmp > (n - 1) / 2) {
            tmp = n - tmp;
          }
          m[tmp]++;
        }
      }
    }
    for (int i = 1; i < (n + 1) / 2; i++)
      cout << m[i] << endl;
  }
}
Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n;
  double n2;

  while (cin >> n && n) {
    int a[5000];
    memset(a, 0, sizeof(a));

    n2 = (n - 1) / 2;

    vector<int> v;
    REP(i, 1, n) {
      int temp = i * i;
      temp %= n;

      vector<int>::iterator ite = find(v.begin(), v.end(), temp);
      if (ite == v.end()) {
        v.push_back(temp);
      }
    }

    rep(i, v.size()) {
      REP(j, i + 1, v.size()) {
        int ans = v[i] - v[j];
        if (ans < 0)
          ans += n;
        if (ans > n2)
          ans = n - ans;
        a[ans]++;
      }
    }

    REP(i, 1, n2 + 1) { cout << a[i] * 2 << endl; }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n;
  double n2;

  while (cin >> n && n) {
    n2 = (n - 1) / 2;

    vector<int> v;
    REP(i, 1, n) {
      int temp = i * i;
      temp %= n;

      vector<int>::iterator ite = find(v.begin(), v.end(), temp);
      if (ite == v.end()) {
        v.push_back(temp);
      }
    }

    map<int, int> m;
    rep(i, v.size()) {
      REP(j, i + 1, v.size()) {
        int ans = v[i] - v[j];
        if (ans < 0)
          ans += n;
        if (ans > n2)
          ans = n - ans;
        m[ans]++;
      }
    }

    REP(i, 1, n2 + 1) { cout << m[i] * 2 << endl; }
  }

  return 0;
}