Gauss Prime UVA - 1415

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

解析:

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

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

或a、b均不为零,而a^2+b^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;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9327874.html