ZOJ - 3483 - Gaussian Prime
先上题目:
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 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的分母。
上代码:
#include <cstdio>
#include <cstring>
#define MAX 20002
#define LL long long
using namespace std; bool f[MAX]; void deal(){
LL n=MAX-;
f[]=f[]=;
memset(f,,sizeof(f));
for(LL i=;i<=n;i++){
if(!f[i]){
for(LL j=i*i;j<=n;j+=i){
f[j]=;
}
}
}
} bool s[][]; bool check(int y,int x){
if(y== && x==) return ;
else if((y== && x!= )|| (y!= && x==)){
int k=x+y;
if(k<) k=-k;
if(k%==%){
return !f[k];
}
return ;
}else{
LL sum=y*y+x*x;
if(!f[sum] && (sum-+)%!=) return ;
//if(!f[sum]) return 1;
}
return ;
} void work(){
int y,x;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
y=i-;
x=j-;
if(check(y,x)) s[i][j]=;
}
}
} int gcd(int a,int b){
return b== ? a : gcd(b,a%b);
} void ask(){
int a,b,c,d,g;
int pr,num;
scanf("%d %d %d %d",&a,&b,&c,&d);
a+=;
b+=;
c+=;
d+=;
pr=;
num=;
for(int i=a;i<=b;i++){
for(int j=c;j<=d;j++){
if(s[i][j]) pr++;
num++;
}
}
g=gcd(num,pr);
printf("%d/%d\n",pr/g,num/g);
} int main()
{
int t;
//freopen("data.txt","r",stdin);
deal();
work();
scanf("%d",&t);
while(t--){
ask();
}
return ;
}
3483
ZOJ - 3483 - Gaussian Prime的更多相关文章
- ZOJ 3483 简单if-else
提醒:答案要约分,不然会错! #include<iostream> #include<cstdio> #include<cstring> #include<a ...
- ZOJ 3707 Calculate Prime S 数论
思路:容易得到s[n]=s[n-1]+s[n-2],也就是fib数. 求第k小的fib质数的也就是第k个质数数-2,当k>2时. 在就是s[n]/x%m=s[n]%(x*m)/x. 代码如下: ...
- 2019/10/27 TZOJ
1001 Gaussian Prime http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=379 ...
- ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem Time Limit: 10 Seconds Memory Limit: 32768 KB A ring is compose of n circ ...
- POJ 1595 Prime Cuts (ZOJ 1312) 素数打表
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...
- Prime Query (ZOJ 3911 线段树)
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a sequen ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 3911 Prime Query(线段树)
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 2015 10月份 月赛 3911 Prime Query
这道题我改啊,改啊,交啊,就对了. #include <stdio.h> #include <stdlib.h> #include <math.h> #includ ...
随机推荐
- B1588 [HNOI2002]营业额统计 set||平衡树
平衡树题,求每个点的前驱,照例可以用set水过...(平衡树还是不会写) 又新学了一个用法: set <int> ::iterator s1; 这样s1就可以直接附为set中的地址了.但是 ...
- DCloud-MUI:utils
ylbtech-DCloud-MUI:utils 1.返回顶部 1.init mui框架将很多功能配置都集中在mui.init方法中,要使用某项功能,只需要在mui.init方法中完成对应参数配置即可 ...
- 微信js sdk上传多张图片
微信js sdk上传多张图片,微信上传多张图片 该案例已tp3.2商城为例 直接上代码: php代码: public function ind(){ $appid="111111111111 ...
- 长脖子鹿省选模拟赛 [LnOI2019SP]快速多项式变换(FPT)
本片题解设计两种解法 果然是签到题... 因为返回值问题T了好久... 第一眼:搜索大水题? 然后...竟然A了 #include<cstdio> #include<queue> ...
- centos 修改网卡信息命令
vi /etc/sysconfig/network-scripts/ifcfg-eth0
- 数据连接类 这里采用mysql
数据库通用操作类,自己参照几个通用类改写的,用起来还是蛮不错.... 这里用的mysql 要是其他数据库自行更改下就好 public class MySqlHelper { public stati ...
- spring 九种设计模式
spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...
- Android Activity has leaked window that was originally added
今天调试程序时log中突然打印这样的错误,但是程序并没有crash,为了不放过一个错误,我决定调查一下. 当时是离开一个activity,然后提示是否退出此界面,接下来就打印此错误: - ::): A ...
- node.js的querystring模块
querystring主要是用于对字符串进行操作和解析.共有四个方法:querystring.parse,querystring.stringify,querystring.escape,querys ...
- eas更改用户组织范围和业务组织范围
表: T_PM_OrgRangeIncludeSubOrg 10 20 30 分别代表 业务组织 行政组织 以及管辖组织.查行政组织,