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,找有多少种方 ...
随机推荐
- js:jquery multiSelect 多选下拉框实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ViewPager禁止滑动以及它与内层滑动控件水平方向上事件冲突的解决方法
一.上图 二.场景描写叙述 最近在做项目的时候.遇到一个怪异的需求,描写叙述例如以下: 1.ViewPager中嵌套3个View,当从View1滑动到View2时禁止ViewPager的滑动事件. 2 ...
- JStorm 是一个分布式实时计算引擎
alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...
- 斐波那契数列(fabnacci)java实现
斐波那契数列定义:From Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Fibonacci_number In math ...
- 基于Android Volley的网络请求工具
基于Android Volley的网络请求工具. 一.说明 AndroidVolley,Android Volley核心库及扩展工程.AndroidVolleySample,网络请求工具示例工程.Re ...
- HDU3757
题意:一些团队因为任务要去避难所,并且每个避难所必须要有团队在,避难所的数量小于等于团队的数量, 团队去避难所的消耗油量与路程成正比,求解最小耗油量.题目来源:2010 Northeastern Eu ...
- (转)server.urlencode有什么用?怎么用?
这个函数主要是为了,不同浏览器的事,如果你传递的参数是中文,有的浏览器就显示出错,如果转换成编码形式,就可以正确传递!当然在IE下2个结果都是一样的,你看不出来区别,用编码来传递参数是一个好习惯,养成 ...
- java 自定义注解以及获得注解的值
1.自定义注解 import java.lang.annotation.*; @Documented @Target(ElementType.FIELD) @Inherited @Retention( ...
- oracle 中 group by 加强
group by rollup(a,b) = group by a,b + group by a + group by null SQL> select deptno,job,sum(sal) ...
- C++ Primer 5th 第7章 类
类的基本思想是数据抽象和封装,定义类就是定义一个抽象数据类型. 类中的所有成员必须在类中声明,也即默认定义在类中的成员全部为声明,除非显式的定义成员函数的函数体.成员函数是在类中声明的,定义可以在类内 ...