Goldbach's Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5567    Accepted Submission(s): 2151

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

题意:给定一个小于2^15的偶n数,求符合p1+p2=n的(p1,p2)有几对。p1,p2与p2,p1视为相同

刚开始想错了,以为p1,p2相同的话实际只需到2^14次即可,后来发现若我p1很小,p2将会很大,早超过2^14了,2^14只是p1的取值范围,实际还得2~2^15。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std; bool prime(const int &n)
{
if(n==1)
return false;
else if(n==2)
return true;
else
{
for (int i=2; i*i<=n; i++)
if(n%i==0)
return false;
}
return true;
} bool sea(const int list[],const int &len,const int &a)
{
int l=1,r=len-1;
int mid;
while (l<=r)
{
mid=(l+r)/2;
if(list[mid]==a)
return true;
else if(list[mid]>a)
{
r=mid-1;
}
else if(list[mid]<a)
{
l=mid+1;
}
}
return false;
} int main(void)
{
int list[3514]={0},i,k=1;
for (i=2; i<=32768; i++)//调试后发现最接近32768的质数在list[3513]处,则数组范围改小,节省空间
{
if(prime(i))
list[k++]=i;
}
int n,ans;
while (~scanf("%d",&n)&&n>=4)
{
ans=0;
for (i=1; list[i]<=n/2; i++)遍历p1
{
if(sea(list,3514,n-list[i]))//找另一半p2
ans++;
}
printf("%d\n",ans);
}
return 0;
}

HDU——1397Goldbach's Conjecture(二分查找+素数打表)的更多相关文章

  1. HDU 3763 CD【二分查找】

    解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...

  2. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  3. hdu 1969 Pie(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1969 Pie Time Limit: 5000/1000 MS (Java/Others)    Me ...

  4. HDU 2136 Largest prime factor (素数打表。。。)

    题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. ...

  5. 查找算法(I) 顺序查找 二分查找 索引查找

    查找 本文为查找算法的第一部分内容,包括了基本概念,顺序查找.二分查找和索引查找.关于散列表和B树查找的内容,待有空更新吧. 基本概念 查找(search)又称检索,在计算机上对数据表进行查找,就是根 ...

  6. Python递归 — — 二分查找、斐波那契数列、三级菜单

    一.二分查找 二分查找也称之为折半查找,二分查找要求线性表(存储结构)必须采用顺序存储结构,而且表中元素顺序排列. 二分查找: 1.首先,将表中间位置的元素与被查找元素比较,如果两者相等,查找结束,否 ...

  7. 数据结构实验7:实现二分查找、二叉排序(查找)树和AVL树

    实验7 学号:      姓名:     专业: 7.1实验目的 (1) 掌握顺序表的查找方法,尤其是二分查找方法. (2) 掌握二叉排序树的建立及查找. 查找是软件设计中的最常用的运算,查找所涉及到 ...

  8. HDU 1397 Goldbach's Conjecture(二分,查找素数)

    题目意思很简单,就是找n=p1+p2的种类数,具体看题目吧. 次重点是查找一定范围内的素数: 重点是用二分查找,不然会超时!!! #include<stdio.h> #include< ...

  9. UVa 10539 (筛素数、二分查找) Almost Prime Numbers

    题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...

随机推荐

  1. OpenGL小试牛刀第二季(粒子模拟)

    效果截图:粒子模拟代码展示:#include "Particle.h" /** 构造函数 */CParticle::CParticle(){ data = NULL; numpar ...

  2. 谷歌浏览器 加安全地址 快捷方式加参数 chrome

    --unsafely-treat-insecure-origin-as-secure="http://192.168.43.17:8080"

  3. python_101_类方法

    class Dog(object): n=333 name='小虎子' def __init__(self,name): self.name=name @classmethod def eat(sel ...

  4. vue跨域处理(vue项目中baseUrl设置问题)

    1.开发环境: 2.生产环境: 然后 const instance = axios.create({ baseURL: process.env.API })

  5. 使用lua做序列化和反序列化

    -- lua对象序列化 function serialize(obj) local lua = "" local t = type(obj) if t == "numbe ...

  6. JS - 生成UUID

    function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw ...

  7. python入门:1-99所有数的和附带等式

    #!/usr/bin/env python # -*- coding:utf-8 -*- #1-99所有数的和的等式 #start(开始,译音:思达二测)sum(合计,译音:桑木)temp(临时雇员, ...

  8. linux系统入门—文件管理

    目录 linux系统入门-文件管理 系统目录结构 目录管理 linux系统入门-文件管理 系统目录结构 几乎所有的计算机操作系统都是使用目录结构组织文件.具体来说就是在一个目录中存放子目录和文件,而在 ...

  9. MongoDB之Replica Sets环境搭建

    最近学习MongoDB,这两天在搭建复制集的时候碰到了不少问题,也踩了好多坑,现在在这里记录下来,以供自己和他人参考 (因为本人是初学者,所以遇到的问题也会比较初级,所以本文也比较适合初学者查阅) 背 ...

  10. LeetCode(287)Find the Duplicate Number

    题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...