HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description
Goldbach’s Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
Output
Each output line should contain an integer number. No other characters should appear in the output.
Sample Input
6
10
12
0
Sample Output
1
2
1
题意:
哥德巴赫猜想:任何偶数n大于或等于4,至少存在一对素数P1和P2,n=p1+p2。p1+p2和p2+p1是一样的。
本题是统计有多少对不同的素数和等于n.
注意用到素数筛选,然后打表就可以得出答案了。
import java.util.Scanner;
public class Main{
static int[] db = new int[65536];
public static void main(String[] args) {
dabiao();
//System.out.println(Math.pow(2, 15));-32768
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
if(n==0){
return ;
}
int tp = 0;
for(int i=1;i<=n/2;i++){
if(db[i]==1&&db[n-i]==1){
tp++;
}
}
System.out.println(tp);
}
}
//快速筛选素数
private static void dabiao() {
for(int i=0;i<db.length;i++){
db[i]=1;
}
db[0]=0;
db[1]=0;
for(int i=2;i<=Math.sqrt(db.length);i++){
for(int j=i+i;j<db.length;j=j+i){
if(db[i]==1){
db[j]=0;
}
}
}
// for(int i=0;i<1000;i++){
// if(db[i]==1){
// System.out.println(i);
// }
// }
}
}
HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)的更多相关文章
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- HDU 1397 Goldbach's Conjecture(二分,查找素数)
题目意思很简单,就是找n=p1+p2的种类数,具体看题目吧. 次重点是查找一定范围内的素数: 重点是用二分查找,不然会超时!!! #include<stdio.h> #include< ...
- HDU 1397 Goldbach's Conjecture【素数打表】
题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...
- 埃氏筛法(快速筛选n以内素数的个数)
给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...
- Light oj-1259 - Goldbach`s Conjecture
1259 - Goldbach`s Co ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- 【NYOJ-187】快速查找素数—— 枚举法、筛选法、打表法
快速查找素数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数. 输入 给出一个正整数数N(N ...
- F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a<=b; a+b==n; a,b都为素数。
/** 题目:F - Goldbach`s Conjecture 链接:https://vjudge.net/contest/154246#problem/F 题意:对一个大于2的偶数n,找有多少种方 ...
随机推荐
- jsonp突破同源策略,实现跨域访问请求
跨域访问问题,相信大家都有遇到过.这是一个很棘手的问题.不过道高一尺,魔高一丈,对于这类问题,总有解决问题的方案.最近我又接触到了这个问题,解决的途径是ajax+jsonp. 说到这个问题,不得不说一 ...
- 【基础练习】【vector】codevs3393 序列倒置
版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 题目非常easy,本质还是练习一下vector 题目描写叙述 Descriptio ...
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- Android BroadcastReceiver实例Demo(有序广播的发送)
上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...
- linux boost 安装
sudo apt-get install libboost-dev 但是,我这样安装以后,编译程序时出现了很多错误,而且都是系统文件的错误.我开始以为是我的boost库版本不对,后来换了好几个版本,都 ...
- ubuntu下mysql中文乱码问题
本来就是想弄个网页往数据库里添加数据的,然后就发现了mysql的中文乱码问题,弄了半天解决方法如下: 首先停mysql服务,编辑配置文件my.cnf $ sudo stop mysql $sudo v ...
- 本篇文章主要是对jquery+ajax+C#实现无刷新操作数据库数据的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
我们知道同步执行和异步执行的区别,为了更好的提高用户的体验,我们都会采用异步方式去处理一些问题,毕竟单线程的同步可能回造成卡死等现象,很不友好,所以可以使用ajax来完成用户的体验,现在我们就来说说如 ...
- asp.mvc获取checkbox、radio、select的值
记录一下在asp.mvc中,提交表单时后台获取checkbox.radio.select值的方法. 1.获取select的值 <select name="type"> ...
- (转)精通 JS正则表达式
精通 JS正则表达式 (精通?标题党 ) 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 ...
- Android学习手记(5) 基本UI布局
1.View和ViewGroup Activity是Android应用程序的基本管理单元,Android的每一个窗口都是通过一个Activity来定义的,但是Activity并不能直接用来显示窗口.我 ...