题意:给出a和b判定是否为高斯素数
解析:
普通的高斯整数i = sqrt(-1)
高斯整数
是素数当且仅当:
a、b中有一个是零,另一个是形为
或其相反数
的素数;
或a、b均不为零,而
为素数。
这题 提取出sqrt(2) 就和普通情况一样了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
int main() {
int t, a, b;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &a, &b);
int ok = 0;
if(a == 0 || b ==0)
{
if(a == 0 && b == 0)
{
cout<< "No" <<endl;
continue;
}
int temp;
if(a == 0)
temp = b;
else
temp = a;
if(temp%4)
{
cout<< "No" <<endl;
continue;
}
else
{
temp -= 3;
for(int i=2; i<=sqrt(temp + 0.5); i++)
if(temp % i == 0)
{
cout<< "No" <<endl;
ok = 1;
break;
}
if(!ok) printf("Yes\n");
}
}
else
{
int flag = 0;
ll x = a*a + 2*b*b;
for (int i = 2; i <= sqrt(x+0.5); i++)
if (x % i == 0) {
printf("No\n");
flag = 1;
break;
}
if (!flag)
printf("Yes\n");
}
}
return 0;
}
转载于:https://www.cnblogs.com/WTSRUVF/p/9327874.html