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. JS实现2,8,10,16进制的相互转换

    // 10进制转为16进制 var a=1234567890; console.log(a.toString(16)) //499602d2 // 16进制转为10进制 var num=parseIn ...

  2. GentleNet使用之详细图解[语法使用增强版]

    目录 第一章 开发环境 第二章 简介 第三章 Gentle.Net-1.5.0 下载文件包介绍 第四章 使用步骤 第五章 源码下载 第一章.开发环境: Vs 2010 + Sql 2005 + Gen ...

  3. 单核CPU并发与非并发测试

    多线程运行程序的目的一般是提高程序运行效率并且能够提高硬件的利用率比如多核CPU,但是如果我们只有单核CPU并发运行程序会怎样呢? 我以两个环境作为对比: 环境A(我本机8c) 环境B(我的云服务器1 ...

  4. Java删除开头和末尾字符串

    //扩展2个String方法 /* * 删除开头字符串 */ public static String trimstart(String inStr, String prefix) { if (inS ...

  5. mysql中添加数据时,报错(incorrect string value:'\xf0\x9f ) 字符转换不正确

    这个问题,原因是UTF-8编码有可能是两个.三个.四个字节.Emoji表情或者某些特殊字符是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 在网上搜了一下解决问题的方案,我选了一 ...

  6. 使用HTML5语义标签时要注意的问题

    header,nav,section,article,aside,figue,figcaption,footer以上这些标签(除figcaption标签外)都是块级标签,为了让这些标签及元素在所有的浏 ...

  7. cocos2x (c++/lua) spine 文件的预加载

    在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...

  8. iOS 绘制1像素的线

    一.Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量.系统在实际渲染到设置时会帮助我们处理Point到P ...

  9. 如何使Recovery分区正常工作

    通常安装完系统后,在进入Clover菜单选择Recovery分区后是进不去的,对于我这种完美强迫症患者来说这是不能忍的,最后,终于在网上找到个简单办法让它工作,废话不多说,上命令: 先找到Recove ...

  10. HDU-1072-Nightmares

    这题可以用dfs写,我们记忆化搜索. 我们定义一个step和time数组,分别表示走到这点的最小步数,time表示走到该点炸弹还剩多少时间. 递归边界一是,如果走到该点,时间等于0,我们就返回. 如果 ...