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,找有多少种方 ...
随机推荐
- iOS开发常用国外网站清单
工欲善其事必先利其器,最近发现临时查找一些东西容易浪费时间,花了点时间整理一下常用的网站,方便以后备用. 国内的code4app,ui4app,cocoachina,oschina,csdn就不说了, ...
- UITouch附加
框架 /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKNode.h 概览 重要提示:这是一个初步的 ...
- XgCalendar日历插件动态添加参数
在使用xgcalendar日历插件的时候,参数数组并非只有类型.显示时间.时区等这些参数,还可以根据extParam自定义参数扩展搜索条件,例如根据用户Id搜索不同用户的日历信息,需要将用户的Id存在 ...
- Config配置文件读写
config文件读写操作(文字说明附加在程序中) App.config文件 <?xml version="1.0" encoding="utf-8" ?& ...
- 从零基础入门JavaScript(2)
在上次的学习当中,我已经对JavaScript有了一定基础上的理解,比如:JavaScript的发展史,JavaScript中变量的命名,各种运算符,以及数据的类型与它们之间的转化.还有就是一些最基本 ...
- Myeclipse+Tomcat安装与配置
一: Myeclipse安装很简单,没什么可说的,下面说一下怎么把英文版的Myeclipse汉化的问题 1.把汉化包解压,将解压后的“language”文件夹,放入Myeclipse\common文件 ...
- mongodb查询之模糊查询
mongodb的模糊查询是用正则表达式来实现的.例子如下: db.COMMODITY_COMMODITY_SHOP.find({name:{$regex :/南京/i}})
- phpmyadmin导出数据库为什么是php文件
你的迅雷在作怪,把它卸载了,或者在迅雷的高级设置中,关闭监听浏览器,就不会触发迅雷下载,就没问题了.或者360浏览器的话,把急速模式改为兼容模式
- android bindService()
bindService简介 Service一般用于不用显示,运行在后台的服务. startService 是我们最常用的启动Service的方法.而如何让service与其他组件通信呢?一般在一个进程 ...
- Linux 所有命令都用不了,只有cd exit能用
原因: 在设置 java环境变量时,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.在命令行下打入下面这段就可以了export PATH=/usr/local/sbin:/us ...