Bank Numbers

Time limit1sMemory limit128 MB

Summary
Determine whether a given Gaussian-integer-like pair (m,n) is a Gaussian prime by checking divisors of m^2+n^2.
Level

Medium6 of 10

Topics
Number theory, Math, Implementation
Solved
No attempts yet

Problem

A bank number is an ordered pair of integers (m,n)(m, n). For example, (1,1)(1, 1), (−2,1)(-2, 1), and (−3,−1)(-3, -1) are bank numbers.

Multiplication of bank numbers is defined by (m,n)⋅(x,y)=(mx−ny,  my+nx)(m, n) \cdot (x, y) = (mx - ny,\; my + nx). For example, (1,1)⋅(−2,1)=(−3,−1)(1, 1) \cdot (-2, 1) = (-3, -1).

If some bank number (x,y)(x, y) satisfies (m,n)⋅(x,y)=(p,q)(m, n) \cdot (x, y) = (p, q), then (m,n)(m, n) is called a divisor of the bank number (p,q)(p, q).

For any bank number (m,n)(m, n), each of (1,0)(1, 0), (0,1)(0, 1), (−1,0)(-1, 0), (0,−1)(0, -1), (m,n)(m, n), (−n,m)(-n, m), (−m,−n)(-m, -n), (n,−m)(n, -m) is a divisor of (m,n)(m, n). If m2+n2>1m^2 + n^2 > 1, these eight bank numbers are all distinct, so every bank number with m2+n2>1m^2 + n^2 > 1 has at least eight divisors.

A bank number (m,n)(m, n) with m2+n2>1m^2 + n^2 > 1 is called prime if it has exactly eight divisors.

Given a bank number, write a program that decides whether it is prime.

Input

The first line contains the number of test cases. Each test case is a single line containing mm and nn of a bank number (m,n)(m, n), separated by a space. (1<m2+n2<200001 < m^2 + n^2 < 20000)

Output

For each test case, print P on its own line if the given bank number is prime, and C otherwise.

Hint

When m2+n2>0m^2 + n^2 > 0, if m2+n2m^2 + n^2 is a common divisor of mp+nqmp + nq and mq−npmq - np, then (m,n)(m, n) is a divisor of (p,q)(p, q), and the converse also holds.

Moreover, if (m,n)⋅(x,y)=(p,q)(m, n) \cdot (x, y) = (p, q), then (m2+n2)(x2+y2)=p2+q2(m^2 + n^2)(x^2 + y^2) = p^2 + q^2.

Examples2

  1. Example 1

    Input
    8
    10 0
    0 2
    -3 0
    4 2
    0 -13
    -4 1
    -2 -1
    3 -1
    
    Expected output
    C
    C
    P
    C
    C
    P
    P
    C
    
  2. Example 2

    Input
    6
    1 1
    2 0
    3 0
    0 -3
    5 0
    0 -5
    
    Expected output
    P
    C
    P
    P
    C
    C