Gauss Prime UVA

题意:给出a和b判定是否为高斯素数

解析:

普通的高斯整数i = sqrt(-1)

高斯整数a+bi是素数当且仅当:

a、b中有一个是零,另一个是形为4n+3或其相反数-(4n+3)的素数;

或a、b均不为零,而a^2+b^2为素数。

这题 提取出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
");
            }

        }
        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
");
                    flag = 1;
                    break;
                }
            if (!flag)
                printf("Yes
");
        }

    }
    return 0;
}
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
原文地址:https://www.cnblogs.com/WTSRUVF/p/9327874.html