題意:給出a和b判定是否為高斯素數
解析:
普通的高斯整數i = sqrt(-1)
高斯整數
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLjlDZlZ2MwgTNzcjM4UjYyAjNmFGZxYzY0YTNwkTZkNzLcV2LcR2LcNzLchGdh12Lcdmcv5SYpRWZtl2apdnLkF2bsBXdvw1LcpDc0RHaiojIsJye.png)
是素數當且僅當:
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