题目描述

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.

输入

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.

输出

Each output line should contain an integer number. No other characters should appear in the output.

样例输入

4
10
16
0

样例输出

1
2
2 题意:给出大于等于4的数p 然后找出俩个素数相加为p p1+p2 和 p2+p1 算一种 这个就是判断 <=n/2 的数 然后访问vis[i]&&vis[n-i] 都是素数 那么就是even number
#include<bits/stdc++.h>

using namespace std;
const int N=4e4;
int prime[N];
bool vis[N];
int cnt=;
void isprime(int n)
{
fill(vis,vis+N,false);
cnt=;
for(int i=; i<n; i++)
{
if(!vis[i])
{
prime[cnt++]=i;
}
for(int j=i+i; j<n; j+=i)
{
vis[j]=true;
}
}
}
int main()
{
int n;
isprime(N);
while(scanf("%d",&n)==,n){
int sum=;
for(int i=;i<=n/;i++){
if(!vis[i]&&!vis[n-i]) sum++;
}
printf("%d\n",sum);
}
return ;
}

直接模拟下 也行 但是复杂度是n^2

#include<bits/stdc++.h>

using namespace std;
const int N=1e6+;
int prime[N];
bool vis[N];
int cnt=;
void isprime(int n)
{
fill(vis,vis+N,false);
cnt=;
for(int i=; i<n; i++)
{
if(!vis[i])
{
prime[cnt++]=i;
}
for(int j=i+i; j<n; j+=i)
{
vis[j]=true;
}
}
}
int main()
{
int n;
while(scanf("%d",&n)==,n){
isprime(n);
int sum=;
int flag=;
for(int i=;i<cnt;i++){
for(int j=i;j<cnt;j++){
if(prime[i]+prime[j]==n){
sum++;
}
}
}
printf("%d\n",sum);
}
return ;
}

问题 C: Goldbach's Conjecture的更多相关文章

  1. Goldbach's Conjecture

     Goldbach's Conjecture Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  2. Poj 2262 / OpenJudge 2262 Goldbach's Conjecture

    1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...

  3. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  4. HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)

    Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...

  5. Goldbach's Conjecture(哥德巴赫猜想)

    Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  6. UVa 543 - Goldbach's Conjecture

    题目大意:给一个偶数,判断是否是两个素数的和. 先用sieve方法生成一个素数表,然后再进行判断即可. #include <cstdio> #include <vector> ...

  7. 【LightOJ1259】Goldbach`s Conjecture(数论)

    [LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...

  8. POJ 2262 Goldbach's Conjecture (打表)

    题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...

  9. 题目1440:Goldbach's Conjecture(哥达巴赫猜想)

    题目链接:http://ac.jobdu.com/problem.php?pid=1440 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  10. Goldbach`s Conjecture(素筛水题)题解

    Goldbach`s Conjecture Goldbach's conjecture is one of the oldest unsolved problems in number theory ...

随机推荐

  1. Android学习笔记_28_手势识别

    一.准备手势库: 使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-10\GestureBuilder).使用Ge ...

  2. Tomcat 服务器体系结构

    connector 监听端口,监听到以后,交给 Engine 引擎 处理,引擎会根据请求找到对应的主机,找到主机后再去找对应的应用. 如果我们将 port 改为 80,那访问的时候就不用输入端口号,因 ...

  3. js面向对象编程——创建对象

    JavaScript对每个创建的对象都会设置一个原型,指向它的原型对象. 当我们用obj.xxx访问一个对象的属性时,JavaScript引擎先在当前对象上查找该属性,如果没有找到,就到其原型对象上找 ...

  4. JavaScript实现快速排序(Quicksort)

    目前,最常见的排序算法大概有七八种,其中"快速排序"(Quicksort)使用得最广泛,速度也较快.它是图灵奖得主 东尼·霍尔(C. A. R. Hoare)于1960时提出来的. ...

  5. 增删改查 报异常org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readO

    可能是Spring配置文件 事务通知里面的方法  与实际方法不匹配 <tx:advice id="advice" transaction-manager="tran ...

  6. Linux的开山篇

    一.Linux的学习方向 1.2Linux运维工程师 1.2.2Linux嵌入式开发工程师 1.2.3在Linux下做各种程序开发    javaEE   大数据    Python  PHP  C/ ...

  7. .NET中获取当前的IP地址

    /// <summary> /// 获取本地IP地址信息 /// </summary> public static string GetAddressIP() { ///获取本 ...

  8. 配置Echarts大全

    由于项目中需要用到Echarts,最近研究了一个星期.网上的教程也挺多的.磕磕碰碰的,难找到合适的例子.都说的马马虎虎.不废话了.开始. 这种上下排列的... 还有这种地图的.(如下) 还有就是配置的 ...

  9. 使用source命令解决mysql导入乱码问题

    设定编码格式:mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  10. SIMD数据并行(三)——图形处理单元(GPU)

    在计算机体系中,数据并行有两种实现路径:MIMD(Multiple Instruction Multiple Data,多指令流多数据流)和SIMD(Single Instruction Multip ...