ZOJ Problem Set - 3483 Gaussian Prime

本文介绍了一种计算复平面上指定区域内高斯素数密度的方法。通过预处理素数并遍历高斯整数来确定是否为高斯素数,并最终以不可约分数形式给出密度结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gaussian Prime

Time Limit: 3 Seconds       Memory Limit: 65536 KB

In number theory, a Gaussian integer is a complex number whose real and imaginary part are both integers. The Gaussian integers, with ordinary addition and multiplication of complex numbers, form an integral domain, usually written as Z[i]. The prime elements of Z[i] are also known as Gaussian primes. Gaussian integers can be uniquely factored in terms of Gaussian primes up to powers of i and rearrangements.

A Gaussian integer a + bi is a Gaussian prime if and only if either:

  • One of ab is zero and the other is a prime number of the form 4n + 3 (with n a nonnegative integer) or its negative -(4n + 3), or
  • Both are nonzero and a2 + b2 is a prime number (which will not be of the form 4n + 3).

0 is not Gaussian prime. 1, -1, i, and -i are the units of Z[i], but not Gaussian primes. 3, 7, 11, ... are both primes and Gaussian primes. 2 is prime, but is not Gaussian prime, as 2 = i(1-i)2.

Your task is to calculate the density of Gaussian primes in the complex plane [x1x2] × [y1y2]. The density is defined as the number of Gaussian primes divided by the number of Gaussian integers.

Input

There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.

Each test case consists of a line containing 4 integers -100 ≤ x1 ≤ x2 ≤ 100, -100 ≤ y1 ≤ y2 ≤ 100.

Output

For each test case, output the answer as an irreducible fraction.

Sample Input
3
0 0 0 0
0 0 0 10
0 3 0 3
Sample Output
0/1
2/11
7/16
References

题意:给你复数(高斯整数)的实部与虚部范围,要你求有多少个高斯素数,高斯素数满足题目里的两个条件,且需要注意条件1和条件2,tem都必须是素数

题解:预处理找出所以素数,然后遍历所有高斯整数,是高斯素数num++

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 100000  
int prim[maxn];


void init()
{
for (int i = 2; i <= maxn; i++)
{
if (!prim[i])
{
for (int j = i + i; j <= maxn; j += i)
{
prim[j] = 1;
}
}
}
}
int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a%b);
}
int num;
int main()
{
int T;
cin >> T;
int x1, x2, y1, y2;
int tem;
while (T--)
{


cin >> x1 >> x2 >> y1 >> y2;
init();
num = 0;
for (int i = x1; i <= x2; i++)
{
for (int j = y1; j <= y2; j++)
{
/*if (i==0&&j==0)
continue;*/
if (j == 0)
{
/*if ((i - 3) % 4 == 0 && i > 0&& prim[i] == 0 || (-i - 3) % 4 == 0 && i < 0 && prim[-i] == 0)
{
num++;
cout << num << " " << i << " " << j << endl;
}*/
if (i < 0)
tem = -i;
else
{
tem = i;
}
if ((tem - 3) % 4 == 0 && prim[tem] == 0)
num++;
}
else if (i == 0)
{
//if ((j - 3) % 4 == 0 && j > 0 && prim[j] == 0 || (-j - 3) % 4 == 0 && j < 0 && prim[-j] == 0)
//{
// num++;
// //cout << num << " " << i << " " << j << endl;
//}
if (j < 0)
tem = -j;
else
{
tem = j;
}
if ((tem - 3) % 4 == 0 && prim[tem] == 0)
num++;
}
else
{
int k = i * i + j * j;
/*int q = 1;
for (int g = 2; g*g<=k; g++)
{
if (k%g == 0)
{
q = 0;
break;
}
}*/
if (prim[k] == 0 && (k - 3) % 4 != 0)
{
num++;
//cout <<num<<" "<< i << " " << j << endl;
}
}
}
}
int sum = (x2 - x1 + 1)*(y2 - y1 + 1);
int gggg = GCD(num, sum);
cout << num / gggg << "/" << sum / gggg << endl;


}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值