ZOJ 3483 Gaussian Prime(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4280

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

代码如下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
//const double pi = atan(1.0)*4;
const double pi = 3.1415926535897932384626;
const double e = exp(1.0);
#define INF 0x3f3f3f3f
//#define INF 1e18
//typedef long long LL;
//typedef __int64 LL;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
#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 main()
{
    int t;
    init();
    scanf("%d",&t);
    int x1, x2, y1, y2;
    while(t--)
    {
        cin>>x1>>x2>>y1>>y2;
        int ans = 0;
        int tem;
        for(int x = x1; x <= x2; x++)
        {
            for(int y = y1; y <= y2; y++)
            {
                if(x == 0)
                {
                    if(y < 0)
                    {
                        tem = -y;
                    }
                    else
                        tem = y;
                    if((tem-3)%4==0 && prim[tem]==0)
                        ans++;
                }
                else if(y == 0)
                {
                    if(x < 0)
                    {
                        tem = -x;
                    }
                    else
                        tem = x;
                    if((tem-3)%4==0 && prim[tem]==0)
                        ans++;
                }
                else
                {
                    tem = x*x+y*y;
                    if(prim[tem]==0 && (tem-3)%4!=0)
                        ans++;
                }
            }
        }
        int tol = (x2-x1+1)*(y2-y1+1);
        int gcd = GCD(ans,tol);
        printf("%d/%d\n",ans/gcd,tol/gcd);
    }
    return 0;
}
时间: 2024-08-11 05:36:30

ZOJ 3483 Gaussian Prime(数学啊 )的相关文章

zoj 3707 Calculate Prime S

fibonacci数列的性质: 1.gcd(fib(n),fib(m))=fib(gcd(n,m)) 证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m时gcd(fib(n),fib(m))=gcd(fib(n-m),fib(m)),递归可 求gcd(fib(n),fib(m))=gcd(fib(k),fib(l)),最后k=l,不然继续递归.K是通过展转相减法求出,易证k=gcd(n,m),所以gcd(fib(n),fib(m)) =fib(gcd(n,m))

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ

ZOJ 3903 Ant(数学,推公示+乘法逆元)

Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's

zoj.3868.GCD Expectation(数学推导&gt;&gt;容斥原理)

GCD Expectation Time Limit: 4 Seconds                                     Memory Limit: 262144 KB Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, x2,…,xm} (each nonempty subset has equal probability to be pick

ZOJ 3710 Friends(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3710 Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends

ZOJ 2988 Conversions(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1987 Conversion between the metric and English measurement systems is relatively simple. Often, it involves either multiplying or dividing by a constant. You must write a program that co

ZOJ - 3866 Cylinder Candy (数学,积分)

Cylinder Candy Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Submit Status Description Edward the confectioner is making a new batch of chocolate covered candy. Each candy center is shaped as a cylinder with radius r mm an

ZOJ 2680 Clock(数学)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1680 There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the smallest angle between the two hands. The angle be

[三分法初探]

[Uva Error Curves]一些二次函数取max的交集还是一个下凸函数.三分即可 #include <bits/stdc++.h> #define maxn 10010 using namespace std; int n, a[maxn], b[maxn], c[maxn]; #define F(i, x) a[i] * x * x + b[i] * x + c[i] inline double cal(double x){ double ret = -1e12; for(int i