题目描述

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. AngularJS 五 过滤器及验证

    AngularJS过滤: AngularJS过滤器允许我们格式化数据以在UI上显示而不改变原始格式. 格式: 一些比较重要的过滤器: Number               Filter       ...

  2. Spring 事务声明无效果(转)

    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常时候 日志是打印了,但是加的事务却没有回滚. 例:      类似这样的方法不会回滚 (一个 ...

  3. spring(二)-反射、动态代理

    主要是对上一篇文章中涉及到的点做补充,欢迎指正! 1.  java反射知识-Spring IOC 依赖注入 Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类:在运行时构造任意一个 ...

  4. Django-rest-framework(一)简单入门使用

    简单的使用 Django-rest-framework 建成DRF,可以帮助我们快速构建出 django的rest full 风格的api接口. 其源码容易理解,所以我们可以很方便的使用. 安装 pi ...

  5. iOS Alamofire插件使用方法

    let parameters = [ " ] Alamofire.request("http://110.185.104.100:8888/skproject/HvLogistic ...

  6. WebSocket 和socket 的区别

    去年光棍节的时候,我写过一篇 quick-cocos2d-x 中的 socket 技术选择:LuaSocket 和 WebSocket .这篇文章介绍了我为何决定在项目中使用 LuaSocket . ...

  7. JSP的小心得

    问题:Web容器(例如Tomcat)是怎么来执行jsp文件的? 首先它会将放在webapps目录下的jsp文件(这里以hello.jsp为例)翻译成hello_jsp.java文件并编译为hello_ ...

  8. 理解 ES6 Generator-next()方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 652. Find Duplicate Subtrees

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  10. 勾股数--Python

    勾股数:勾股数又名毕氏三元数 .勾股数就是可以构成一个直角三角形三边的一组正整数.勾股定理:直角三角形两条直角边a.b的平方和等于斜边c的平方(a²+b²=c²) 要求:输出1000以内的勾股数 fr ...