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

// 17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct E {
  string t;
  int d;
};

struct S {
  string v;
  int r, d;
  bool operator<(S a) const { return d > a.d; }
};

int main() {
  for (int n, mm, cap; cin >> n >> mm >> cap, n | mm | cap;) {
    cap *= 10;
    string src, dest;
    cin >> src >> dest;
    map<string, vector<E>> m;
    for (int i = 0; i < n; i++) {
      string c[2];
      int d;
      cin >> c[0] >> c[1] >> d;
      for (int j = 0; j < 2; j++) {
        E ce = {c[j ^ 1], d};
        m[c[j]].push_back(ce);
      }
    }
    set<string> gs;
    for (int i = 0; i < mm; i++) {
      string s;
      cin >> s;
      gs.insert(s);
    }
    priority_queue<S> que;
    S is = {src, cap, 0};
    que.push(is);
    map<string, map<int, int>> p;
    while (!que.empty()) {
      S c = que.top();
      if (c.v == dest)
        break;
      que.pop();
      map<int, int>::iterator it = p[c.v].upper_bound(c.r - 1);
      if (it != p[c.v].end() && it->second <= c.d)
        continue;
      p[c.v][c.r] = c.d;
      for (int i = 0; i < m[c.v].size(); i++) {
        if (m[c.v][i].d <= c.r) {
          S n = {m[c.v][i].t, c.r - m[c.v][i].d, c.d + m[c.v][i].d};
          if (gs.count(n.v)) {
            n.r = cap;
          }
          que.push(n);
        }
      }
    }
    cout << (que.empty() ? -1 : que.top().d) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct E {
  string t;
  int d;
};

struct S {
  string v;
  int r, d;
  bool operator<(S a) const { return d > a.d; }
};

int main() {
  for (int n, mm, cap; cin >> n >> mm >> cap, n | mm | cap;) {
    cap *= 10;
    string src, dest;
    cin >> src >> dest;
    map<string, vector<E>> m;
    for (int i = 0; i < n; i++) {
      string c[2];
      int d;
      cin >> c[0] >> c[1] >> d;
      for (int j = 0; j < 2; j++) {
        E ce = {c[j ^ 1], d};
        m[c[j]].push_back(ce);
      }
    }
    set<string> gs;
    for (int i = 0; i < mm; i++) {
      string s;
      cin >> s;
      gs.insert(s);
    }
    priority_queue<S> que;
    S is = {src, cap, 0};
    que.push(is);
    map<string, map<int, int>> p;
    while (!que.empty()) {
      S c = que.top();
      if (c.v == dest)
        break;
      que.pop();
      map<int, int>::iterator it = p[c.v].upper_bound(c.r);
      if (it != p[c.v].end() && it->second <= c.d)
        continue;
      p[c.v][c.r] = c.d;
      for (int i = 0; i < m[c.v].size(); i++) {
        if (m[c.v][i].d <= c.r) {
          S n = {m[c.v][i].t, c.r - m[c.v][i].d, c.d + m[c.v][i].d};
          if (gs.count(n.v)) {
            n.r = cap;
          }
          que.push(n);
        }
      }
    }
    cout << (que.empty() ? -1 : que.top().d) << endl;
  }
  return 0;
}
Problem p00899 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int N = 14;
const int INF = 1e9;
string str[20], s[20];
int dp[20][1 << 15], ok[20][20];
int n;
int check(int x, int y) {
  int i, j, r = 0, l1 = s[x].size(), l2 = s[y].size();
  for (i = 0; i < l1; i++) {
    for (j = 0; i + j < l1 && j < l2; j++)
      if (s[x][i + j] != s[y][j])
        break;
    if (i + j == l1 && r < j)
      r = j;
  }
  return r;
}
bool ck(int x, int y) {
  int l1 = str[x].size(), l2 = str[y].size();
  int i, j;
  for (i = 0; i < l2; i++) {
    for (j = 0; j < l1 && i + j < l2; j++)
      if (str[x][j] != str[y][i + j])
        break;
    if (j == l1)
      return true;
  }
  return false;
}
int dfs(int x, int zt) {

  if (dp[x][zt] != -1)
    return dp[x][zt];
  dp[x][zt] = INF;
  int i;
  for (i = 0; i < n; i++) {
    if ((zt & (1 << i)) && i != x) {
      int t = dfs(i, zt - (1 << x)) + s[x].size() - ok[i][x];

      if (dp[x][zt] > t)
        dp[x][zt] = t;
    }
  }

  return dp[x][zt];
}
int main() {
  int i, j, k;
  while (~scanf("%d", &n)) {
    memset(dp, -1, sizeof(dp));
    if (!n)
      break;
    for (i = 0; i < n; i++)
      cin >> str[i];
    k = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (i != j && ck(i, j))
          break;
      }
      if (j == n)
        s[k++] = str[i];
    }

    n = k;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        ok[i][j] = check(i, j);
      }
    }

    for (i = 0; i < n; i++)
      dp[i][1 << i] = s[i].size();
    int L = (1 << n);
    for (i = 0; i < n; i++)
      dfs(i, L - 1);
    int r = INF;
    for (i = 0; i < n; i++)
      if (r > dp[i][L - 1])
        r = dp[i][L - 1];
    printf("%d\\n", r);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  int i, j, T, n, m;
  long long sum;
  int f[17][17];
  string s[17];
  while (1) {
    scanf("%d", &T);
    sum = 0;
    for (i = 1; i <= T; i++) {
      cin >> s[i];
      sum += s[i].size();
    }
    for (i = 1; i <= T; i++) {
      for (j = 1; j <= T; j++) {
        if (i == j)
          f[i][j] = 0;
        else {
          m = 0;
          for (int t = s[i].size() < s[j].size() ? s[i].size() : s[j].size();
               t >= 1; t--) {
            if (s[j].compare(0, t, s[i], s[i].size() - t, t) == 0) {
              m = t;
              break;
            }
          }
          f[i][j] = m;
        }
      }
    }
    int t;
    for (i = 1; i <= T; i++)
      for (j = 1; j <= T; j++)
        if (i != j)
          for (t = 1; t <= T; t++) {
            if (t != i && t != j)
              f[i][j] = max(f[i][j], f[i][t] + f[t][j]);
          }
    int x = 0;
    for (i = 1; i <= T; i++)
      for (j = 1; j <= T; j++)
        if (f[i][j] > x)
          x = f[i][j];
    printf("%d\\n", sum - x);
  }
}
/*freopen(窶忸xx.in窶?窶搜窶?stdin);
freopen(窶忸xx.out窶?窶掫窶?stdout);*/
Problem p00899 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reep(i, a, b) for (int i = a; i < b; i++)
#define INF 0x3f3f3f3f

bool check(string a, string b) {
  if (a.size() > b.size())
    return false;
  rep(i, b.size()) {
    if (b.substr(i, a.size()) == a)
      return true;
  }
  return false;
}

int overlap(string &a, string &b) {
  rep(i, a.size()) {
    if (a.substr(i) == b.substr(0, a.substr(i).size()))
      return a.size() - i;
  }
  return 0;
}

int dp[1 << 14][14];
int ol[14][14];

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    rep(i, n) { cin >> v[i]; }
    vector<int> use(n);
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        if (check(v[i], v[j])) {
          use[i] = 1;
        }
      }
    }
    vector<string> w;
    rep(i, n) {
      if (use[i] == 0) {
        w.push_back(v[i]);
      }
    }
    int nn = w.size();
    rep(i, 1 << nn) rep(j, nn) dp[i][j] = INF;
    rep(i, nn) { dp[1 << i][i] = w[i].size(); }
    rep(i, nn) {
      rep(j, nn) {
        if (i == j)
          continue;
        ol[i][j] = overlap(w[i], w[j]);
      }
    }
    rep(i, 1 << nn) {
      rep(j, nn) {
        if (dp[i][j] == INF)
          continue;
        rep(k, nn) {
          if (i & (1 << k))
            continue;
          // int ol = overlap(w[j], w[k]);
          dp[i | (1 << k)][k] =
              min(dp[i | (1 << k)][k], dp[i][j] - ol[j][k] + (int)w[k].size());
        }
      }
    }
    int ans = INF;
    rep(i, nn) { ans = min(ans, dp[(1 << nn) - 1][i]); }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reep(i, a, b) for (int i = a; i < b; i++)
#define INF 0x3f3f3f3f

bool check(string a, string b) {
  if (a.size() > b.size())
    return false;
  rep(i, b.size()) {
    if (b.substr(i, a.size()) == a)
      return true;
  }
  return false;
}

int overlap(string &a, string &b) {
  rep(i, a.size()) {
    if (a.substr(i) == b.substr(0, a.substr(i).size()))
      return a.size() - i;
  }
  return 0;
}

int dp[1 << 14][14];

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    rep(i, n) { cin >> v[i]; }
    vector<int> use(n);
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        if (check(v[i], v[j])) {
          use[i] = 1;
        }
      }
    }
    vector<string> w;
    rep(i, n) {
      if (use[i] == 0) {
        w.push_back(v[i]);
      }
    }
    int nn = w.size();
    rep(i, 1 << nn) rep(j, nn) dp[i][j] = INF;
    rep(i, nn) { dp[1 << i][i] = w[i].size(); }
    rep(i, 1 << nn) {
      rep(j, nn) {
        if (dp[i][j] == INF)
          continue;
        rep(k, nn) {
          if (i & (1 << k))
            continue;
          int ol = overlap(w[j], w[k]);
          dp[i | (1 << k)][k] =
              min(dp[i | (1 << k)][k], dp[i][j] - ol + (int)w[k].size());
        }
      }
    }
    int ans = INF;
    rep(i, nn) { ans = min(ans, dp[(1 << nn) - 1][i]); }
    cout << ans << endl;
  }
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int d, m, n, L = 150, ans;
  cin >> d;
  while (d--) {
    cin >> m >> n;
    ans = 0;
    for (int i = -L; i < L; i++)
      for (int j = -L; j < L; j++) {
        if (!i && !j)
          continue;
        int a = i * i + j * j;
        if (!((i * m + j * n) % a) && !((i * n - j * m) % a))
          ans++;
      }
    if (ans == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int d, m, n;
  set<P1> s;
  cin >> d;
  while (d--) {
    cin >> m >> n;
    int L = abs(m) + abs(n);
    for (int i = -L; i <= L; i++) {
      for (int j = -L; j <= L; j++) {
        for (int k = -L; k <= L; k++) {
          for (int l = -L; l <= L; l++) {
            int M = i * k - j * l;
            int N = i * l + j * k;
            if (m == M && n == N)
              s.insert(P1(P(i, j), P(k, l)));
          }
        }
      }
    }
    //    cout<<s.size()<<endl;
    if (s.size() == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
    s.clear();
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> Ginkgo;

map<int, int> sq;

int main() {
  for (int i = 0; i < 200; i++)
    sq[i * i] = i;

  int N;
  cin >> N;
  while (N--) {
    set<Ginkgo> divs;
    int p, q;
    cin >> p >> q;
    for (int i = 1; i <= p * p + q * q; i++) {
      for (int m = -200; m < 200; m++)
        if (i - m * m >= 0) {
          int n2 = i - m * m;
          if (sq.count(n2) == 0)
            continue;

          for (auto n : {-sq[n2], sq[n2]}) {
            if ((m * p + n * q) % i == 0 && (m * q - n * p) % i == 0) {
              divs.insert({n, m});
            }
          }
        }
    }
    cout << (divs.size() == 8 ? "P" : "C") << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> Ginkgo;

int main() {
  int N;
  cin >> N;
  while (N--) {
    set<Ginkgo> divs;
    int p, q;
    cin >> p >> q;
    for (int i = 1; i <= p * p + q * q; i++) {
      for (int m = -200; m < 200; m++)
        if (i - m * m >= 0) {
          for (int n = -200; n < 200; n++)
            if (n * n + m * m == i) {
              if ((m * p + n * q) % i == 0 && (m * q - n * p) % i == 0) {
                divs.insert({n, m});
              }
            }
        }
    }
    cout << (divs.size() == 8 ? "P" : "C") << endl;
  }
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  auto div = [](int m, int n, int p, int q) {
    int mpnq = m * p + n * q;
    int mqnp = m * q - n * p;
    int mn = m * m + n * n;
    return (mpnq % mn == 0) && (mqnp % mn == 0);
  };
  int line;
  cin >> line;
  while (0 < line--) {
    int m, n;
    cin >> m >> n;
    int count = 0;
    for (int p = -150; p < 150; p++)
      for (int q = -150; q < 150; q++) {
        if (p == 0 && q == 0)
          continue;
        if (div(p, q, m, n))
          count++;
      }
    cout << ((count == 8) ? "P" : "C") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define MAX 20000

vector<pair<int, int>> init_v(int m, int n) {
  vector<pair<int, int>> init;
  init.push_back(make_pair(1, 0));
  init.push_back(make_pair(0, 1));
  init.push_back(make_pair(-1, 0));
  init.push_back(make_pair(0, -1));
  init.push_back(make_pair(m, n));
  init.push_back(make_pair(-n, m));
  init.push_back(make_pair(-m, -n));
  init.push_back(make_pair(n, -m));
  return init;
}

vector<pair<int, int>> generator_ginko(int p, int q, vector<bool> num) {
  vector<pair<int, int>> gen;
  int pq = p * p + q * q;
#ifdef D
  cout << "p**2 + q**2 = " << pq << endl;
#endif
  for (int i = 0; i < pq; i++) {
    if (num[i]) {
      for (int j = 0; j < pq; j++) {
        int ij = i * j;
        if (num[j] && pq == ij) {
          gen.push_back(make_pair(i, j));
        }
      }
    }
  }
  return gen;
}

void print_vec(vector<pair<int, int>> v) {
  for (auto x : v) {
    cout << "(" << x.first << "," << x.second << "), ";
  }
  cout << endl;
}

int main(void) {
  vector<bool> num(MAX, false);
  {
    for (int i = 0; i < MAX; i++) {
      for (int j = 0; j < MAX; j++) {
        int tmp = i * i + j * j;
        if (tmp < MAX)
          num[tmp] = true;
        else
          break;
      }
    }
    num[0] = false;
  }
#ifdef D
  for (int i = 0; i < 20; i++)
    cout << i << ":" << num[i] << endl;
#endif

  int z;
  cin >> z;
  vector<bool> ans;
  for (int g = 0; g < z; g++) {
    int m, n;
    cin >> m >> n;
    vector<pair<int, int>> init, gen;
    //		init = init_v(m, n);
    gen = generator_ginko(m, n, num);
#ifdef D
    print_vec(gen);
#endif
    if (gen.size() == 0)
      ans.push_back(true);
    else
      ans.push_back(false);
  }
  for (auto f : ans) {
    if (f)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

signed main() {
  int D;
  cin >> D;
  while (D--) {
    int m, n;
    cin >> m >> n;

    int cnt = 0;
    rep(p, -300, 300) rep(q, -300, 300) {
      if (p == 0 && q == 0)
        continue;
      int sum = p * p + q * q;
      if ((m * p + n * q) % sum)
        continue;
      if ((n * p - m * q) % sum)
        continue;
      cnt++;
    }
    cout << (cnt == 8 ? "P" : "C") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

signed main() {
  int D;
  cin >> D;
  while (D--) {
    int m, n;
    cin >> m >> n;

    int cnt = 0;
    rep(p, -1500, 1500) rep(q, -1500, 1500) {
      if (p == 0 && q == 0)
        continue;
      int sum = p * p + q * q;
      if ((m * p + n * q) % sum)
        continue;
      if ((n * p - m * q) % sum)
        continue;
      cnt++;
    }
    cout << (cnt == 8 ? "P" : "C") << endl;
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {

  int t;
  cin >> t;

  while (t--) {
    int p, q;
    cin >> p >> q;
    int tmp = p * p + q * q, cnt = 0;

    for (int n = 0; n * n <= tmp; n++) {
      for (int m = 0; m * m <= tmp; m++) {
        if (m * m + n * n < 1)
          continue;
        int res = gcd(m * p + n * q, m * q - n * p);
        if ((m * p + n * q) % (m * m + n * n) == 0 &&
            (m * q - n * p) % (m * m + n * n) == 0)
          cnt++;
      }
    }

    if (cnt * 2 <= 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {

  int t;
  cin >> t;

  while (t--) {
    int p, q;
    cin >> p >> q;
    int tmp = p * p + q * q, cnt = 0;

    for (int n = 0; n * n <= tmp; n++) {
      for (int m = 0; m <= tmp; m++) {
        if (m * m + n * n < 1)
          continue;
        int res = gcd(m * p + n * q, m * q - n * p);
        if ((m * p + n * q) % (m * m + n * n) == 0 &&
            (m * q - n * p) % (m * m + n * n) == 0)
          cnt++;
      }
    }

    if (cnt * 2 <= 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  for (size_t i = 0; i < N; i++) {
    long double p, q;
    cin >> p >> q;
    int count = 0;
    for (long double m = 0; m < 145; m++) {
      for (long double n = 0; n < 145; n++) {
        if (m || n) {
          long double x = (m * q - p * n) / (m * m + n * n);
          long double y = (p * m + n * q) / (m * m + n * n);
          if (x == (long long int)x && y == (long long int)y) {
            count++;
          }
        }
      }
    }
    if (count > 4) {
      cout << 'C' << endl;
    } else {
      cout << 'P' << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void c(pair<int, int> mn) {
  int count = 0;
  for (int m = 0; m < 145; m++) {
    for (int p = 0; p < 145; p++) {
      for (int n = 0; n < 145; n++) {
        for (int q = 0; q < 145; q++) {
          if (m * p + n * q == mn.first && m * q - n * p == mn.second) {
            count++;
          }
          if (count >= 2) {
            cout << 'C' << endl;
            return;
          }
        }
      }
    }
  }
  cout << 'P' << endl;
  return;
}

int main() {
  int N;
  cin >> N;
  for (size_t i = 0; i < N; i++) {
    pair<int, int> mn;
    cin >> mn.first >> mn.second;
    c(mn);
  }
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int absmax(int p, int q) {
  int a, b;
  a = p;
  b = q;
  if (a < 0)
    a *= -1;
  if (b < 0)
    b *= -1;
  if (a < b)
    return b;
  return a;
}

struct pairs {
  int p, q;
  bool operator==(const pairs &right) const {
    return (p == right.p && q == right.q);
  }
  bool operator<(const pairs &right) const {
    return (p == right.p ? q < right.q : p < right.p);
  }
  pairs operator*(const pairs &right) const {
    pairs ret;
    ret.p = p * right.p - q * right.q;
    ret.q = p * right.q + q * right.p;
    return ret;
  }
};

int main(void) {
  //	FILE* fp_in = freopen("data.txt", "r", stdin);
  int N;
  cin >> N;
  string ans[] = {"C", "P"};

  for (int i = 0; i < N; i++) {
    pairs p, q;
    vector<pairs> sp;
    cin >> p.p >> p.q;
    for (q.p = -absmax(p.p, p.q); q.p <= absmax(p.p, p.q); q.p++) {
      for (q.q = -absmax(p.p, p.q); q.q <= absmax(p.p, p.q); q.q++) {
        int tmp = q.p * q.p + q.q * q.q;
        if (tmp > 0 && ((q.p * p.p + q.q * p.q) % tmp == 0) &&
            (q.p * p.q - q.q * p.p) % tmp == 0) {
          sp.push_back(q);
        }
      }
    }
    cout << ans[sp.size() == 8] << endl;
  }
  //	while(1){}
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int absmax(int p, int q) {
  int a, b;
  a = p;
  b = q;
  if (a < 0)
    a *= -1;
  if (b < 0)
    b *= -1;
  if (a < b)
    return b;
  return a;
}

struct pairs {
  int p, q;
  bool operator==(const pairs &right) const {
    return (p == right.p && q == right.q);
  }
  bool operator<(const pairs &right) const {
    return (p == right.p ? q < right.q : p < right.p);
  }
  pairs operator*(const pairs &right) const {
    pairs ret;
    ret.p = p * right.p - q * right.q;
    ret.q = p * right.q + q * right.p;
    return ret;
  }
};

int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);
  int N;
  cin >> N;
  string ans[] = {"C", "P"};

  for (int i = 0; i < N; i++) {
    pairs p, q, r;
    set<pairs> sp;
    cin >> p.p >> p.q;
    for (q.p = -absmax(p.p, p.q); q.p <= absmax(p.p, p.q); q.p++) {
      for (q.q = -absmax(p.p, p.q); q.q <= absmax(p.p, p.q); q.q++) {
        for (r.p = -absmax(p.p, p.q); r.p <= absmax(p.p, p.q); r.p++) {
          for (r.q = -absmax(p.p, p.q); r.q <= absmax(p.p, p.q); r.q++) {
            if (q * r == p)
              sp.insert(q);
          }
        }
      }
    }
    //	cout << sp.size() << endl;
    cout << ans[sp.size() == 8] << endl;
  }
  // while(1){}
  return 0;
}