Ultraviolet A 1415-Gauss prime
Question Link
Given a + bi, determine whether it is a Gaussian prime number, I = SQRT (-2 ).
Train of Thought: the common Gaussian prime I = SQRT (-1), the judgment method is:
1. If A or B is 0, judge another prime number in the form of 4 * n + 3 (using the squared theorem of ferma)
2. If neither a nor B is 0, judge whether a ^ 2 + B ^ 2 is a prime number.
Then this question is extracted from SQRT (2), which is the same as the basic situation.
If both A and B are not 0, judge whether a ^ 2 + 2 B ^ 2 is a prime number.
1 is a little different. It is in the form of x ^ 2 + 2 y ^ 2. It is not applicable to the ferma sum of squares theorem. Because A and B have a maximum of 1 W, they can be enumerated completely, determine whether it can be split into the form of x ^ 2 + 2 * y ^ 2. This solves the problem.
Code:
#include <stdio.h>#include <string.h>#include <math.h>const int N = 20005;int t, a, b, prime[N], pn = 0, vis[N];bool judge() { if (a == 0) {int m = sqrt(m);for (int i = 0; i <= m; i++) { double t = sqrt((b - i * i) * 1.0 / 2); if (ceil(t) == floor(t)) return true;}return false; } int tmp = a * a + 2 * b * b; for (int i = 0; i < pn && prime[i] < tmp; i++)if (tmp % prime[i] == 0) return false; return true;}int main() { vis[1] = 1; for (int i = 2; i < N; i++) {if (vis[i]) continue;prime[pn++] = i;for (int j = i * i; j < N; j += i) { vis[j] = 1;} } scanf("%d", &t); while (t--) {scanf("%d%d", &a, &b);printf("%s\n", judge()?"Yes":"No"); } return 0;}