Problem p00142 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const int mod = 1e9 + 7;

int main() {
  vi v(10001, 1);
  v[0] = v[1] = 0;
  for (int i = 2; i < 10001; i++) {
    if (v[i] == 1) {
      for (int j = i * 2; j < 10001; j += i)
        v[j] = 0;
    }
  }
  int n;
  while (/*cin >> n*/ scanf("%d", &n) != EOF && n > 0) {
    if (n % 4 == 3 && v[n]) {
      for (int i = 0; i < (n - 1) / 2; i++)
        printf("%d\\n", (n - 1) / 2 - 1);
      continue;
    }
    set<int> s;
    map<int, int> m;
    for (int i = 1; i < n; i++)
      s.insert(i * i % n);
    // for(auto it :s) cout<<it<<endl;
    for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {
      // for(set<int>::iterator it2 = s.begin(); it2 != s.end(); it2++){
      set<int>::iterator it2 = it;
      ++it2;
      while (it2 != s.end()) {
        int a = *it - *it2;
        if (a < 0)
          a += n;
        if (a > (n - 1) / 2)
          a = n - a;
        m[a] += 2;
        ++it2;
      }
    }
    for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
      // cout<</*it->fi<<" "<<*/it->se<<endl;
      printf("%d\\n", it->se);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const int mod = 1e9 + 7;

int main() {
  int n;
  while (/*cin >> n*/ scanf("%d", &n) != EOF && n > 0) {
    set<int> s;
    map<int, int> m;
    for (int i = 1; i < n; i++)
      s.insert(i * i % n);
    // for(auto it :s) cout<<it<<endl;
    for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {
      // for(set<int>::iterator it2 = s.begin(); it2 != s.end(); it2++){
      set<int>::iterator it2 = it;
      ++it2;
      while (it2 != s.end()) {
        int a = *it - *it2;
        if (a < 0)
          a += n;
        if (a > (n - 1) / 2)
          a = n - a;
        m[a] += 2;
        ++it2;
      }
    }
    for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
      // cout<</*it->fi<<" "<<*/it->se<<endl;
      printf("%d\\n", it->se);
    }
  }
}