ZOJ - 3483 - Gaussian Prime
先上题目:
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 a, b 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 [x1, x2] × [y1, y2]. 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
- http://en.wikipedia.org/wiki/Gaussian_integer
- Weisstein, Eric W. "Gaussian Prime." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/GaussianPrime.html
题意:告诉你一种数的定义,这种数是一个复数,告诉你题目的整个区间的范围,然后给你一个区间,问这个区间里面这种数的密度是多少(这种数比上区间里面的数的总个数)?
题目给的范围比较小,所以可以先预处理把区间里面的这种数都标记出来,然后对于每一次询问就搜一次。
这题需要注意的地方是这种数的定义。这里现需要把0~20000的素数都筛出来,然后根据定义把区间的这种数都找出来就行了。
还有一个需要注意的地方是如果分子是零的时候需要输出的是0/1,而不是0/大于1的分母。
上代码:
1 #include <cstdio> 2 #include <cstring> 3 #define MAX 20002 4 #define LL long long 5 using namespace std; 6 7 bool f[MAX]; 8 9 void deal(){ 10 LL n=MAX-1; 11 f[0]=f[1]=1; 12 memset(f,0,sizeof(f)); 13 for(LL i=2;i<=n;i++){ 14 if(!f[i]){ 15 for(LL j=i*i;j<=n;j+=i){ 16 f[j]=1; 17 } 18 } 19 } 20 } 21 22 bool s[202][202]; 23 24 bool check(int y,int x){ 25 if(y==0 && x==0) return 0; 26 else if((y==0 && x!=0 )|| (y!=0 && x==0)){ 27 int k=x+y; 28 if(k<0) k=-k; 29 if(k%4==3%4){ 30 return !f[k]; 31 } 32 return 0; 33 }else{ 34 LL sum=y*y+x*x; 35 if(!f[sum] && (sum-3+4)%4!=0) return 1; 36 //if(!f[sum]) return 1; 37 } 38 return 0; 39 } 40 41 void work(){ 42 int y,x; 43 for(int i=0;i<=200;i++){ 44 for(int j=0;j<=200;j++){ 45 y=i-100; 46 x=j-100; 47 if(check(y,x)) s[i][j]=1; 48 } 49 } 50 } 51 52 int gcd(int a,int b){ 53 return b==0 ? a : gcd(b,a%b); 54 } 55 56 void ask(){ 57 int a,b,c,d,g; 58 int pr,num; 59 scanf("%d %d %d %d",&a,&b,&c,&d); 60 a+=100; 61 b+=100; 62 c+=100; 63 d+=100; 64 pr=0; 65 num=0; 66 for(int i=a;i<=b;i++){ 67 for(int j=c;j<=d;j++){ 68 if(s[i][j]) pr++; 69 num++; 70 } 71 } 72 g=gcd(num,pr); 73 printf("%d/%d\n",pr/g,num/g); 74 } 75 76 int main() 77 { 78 int t; 79 //freopen("data.txt","r",stdin); 80 deal(); 81 work(); 82 scanf("%d",&t); 83 while(t--){ 84 ask(); 85 } 86 return 0; 87 }