问题 C: Goldbach's Conjecture
题目描述
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的更多相关文章
- Goldbach's Conjecture
Goldbach's Conjecture Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...
- Goldbach's Conjecture(哥德巴赫猜想)
Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- UVa 543 - Goldbach's Conjecture
题目大意:给一个偶数,判断是否是两个素数的和. 先用sieve方法生成一个素数表,然后再进行判断即可. #include <cstdio> #include <vector> ...
- 【LightOJ1259】Goldbach`s Conjecture(数论)
[LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...
- POJ 2262 Goldbach's Conjecture (打表)
题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...
- 题目1440:Goldbach's Conjecture(哥达巴赫猜想)
题目链接:http://ac.jobdu.com/problem.php?pid=1440 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- Goldbach`s Conjecture(素筛水题)题解
Goldbach`s Conjecture Goldbach's conjecture is one of the oldest unsolved problems in number theory ...
随机推荐
- 【luogu P1462 通往奥格瑞玛的道路】 题解
题目链接:https://www.luogu.org/problemnew/show/P1462 记住HP=0也叫死. #include <queue> #include <cstd ...
- 【luogu P1455 搭配购买】 题解
题目链接:https://www.luogu.org/problemnew/show/P1455 一句话题目做法:并查集合并+01背包 启示:要每次再find一遍.路径压缩会快.因为合并的时候如果是1 ...
- Android学习笔记_43_网络通信之文件断点上传
1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...
- scala性能测试
主要对比scala 的for, while循环,以及和java for while循环作对比 scala代码 object TestScalaClass { var maxindex = 100000 ...
- NDK下载地址
官方下载地址 http://developer.android.com/ndk/downloads/index.html 没有提供旧版下载链接,只能修改链接方式下载 http://dl.google. ...
- 重置按钮_reset
function formreset(form){ for(var i=0;i<frmMain.length;i++){ if(frmMain.item(i).type=="text& ...
- redis sentinel搭建以及在jedis中使用
一.redis主从搭建 1.搭建redis master 1>redis安装 mkdir -p /usr/local/webserver/redis //安装目录 cd /usr/local/w ...
- OGG抽取进程异常问题排查一例
1.问题现象抽取进程常常running,但是没有新产生trail文件,lag比较大 GGSCI (xxxdb) > info all Program Status Group Lag at Ch ...
- iOS 从0到1搭建高可用App框架
iOS 从0到1搭建高可用App框架 最近在搭建新项目的iOS框架,一直在思考如何才能搭建出高可用App框架,能否避免后期因为代码质量问题的重构.以前接手过许多“烂代码”,架构松散,底层混乱,缺少规范 ...
- Hibernate知识点小结汇总
Hibernate部分 1.为什么要使用Hibernate开发你的项目呢?Hibernate的开发流程是怎么样的? 为什么要使用 ①.对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复 ...