poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 34323 | Accepted: 13169 |
Description
Every even number greater than 4 can be written as the sum of two odd prime numbers.
For example:
8 = 3 + 5. Both 3 and 5 are odd prime numbers. 20 = 3 + 17 = 7 + 13. 42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.) Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.
Input
Output
Sample Input
8
20
42
0
Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37
Source
/**
Judge Status:Accepted Memory:4852K
Time:344MS Language:G++
Code Lenght:797B Author:cj
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> #define N 1000000
using namespace std; int prim[N+];
int arrPrim[N+]; int main()
{
int i,j;
memset(prim,,sizeof(prim));
for(i=;i*i<=N;i++) //素数筛选法
{
if(!prim[i])
{
for(j=;j*i<=N;j++)
{
prim[j*i]=;
}
}
}
int arr_cnt=;
for(i=;i<=N;i++)
{
if(!prim[i]) arrPrim[arr_cnt++]=i; //一次遍历赛选出来的素数存入数组,遍历用
}
int n;
while(~scanf("%d",&n)&&n)
{
for(i=;i<arr_cnt;i++)
{
if(!prim[n-arrPrim[i]])
{
printf("%d = %d + %d\n",n,arrPrim[i],n-arrPrim[i]);
break;
}
}
}
return ;
}
poj 2262 Goldbach's Conjecture(素数筛选法)的更多相关文章
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj 2262 Goldbach's Conjecture——筛质数(水!)
题目:http://poj.org/problem?id=2262 大水题的筛质数. #include<iostream> #include<cstdio> #include& ...
- POJ 2262 Goldbach's Conjecture (打表)
题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...
- POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
http://poj.org/problem?id=2262 题意: 哥德巴赫猜想,把一个数用两个奇素数表示出来. 思路:先用Eratosthenes筛法打个素数表,之后枚举即可. #include& ...
- POJ 2262 Goldbach's Conjecture 数学常识 难度:0
题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分 ...
- poj 2262 Goldbach's Conjecture
素数判定...很简单= =.....只是因为训练题有,所以顺便更~ #include<cstdio> #include<memory.h> #define maxn 50000 ...
- POJ 2262 Goldbach's Conjecture(素数相关)
POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示 ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
随机推荐
- hdu 4619 最大匹配问题
思路:把所有涉及到的点按(x+y)的奇偶分成两部分点,对所有的1*2的骨牌,都有(x+y)为偶数的建到奇数的边.求一次最大匹配,就是答案. #include<iostream> #incl ...
- Netbackup磁带过期处理
bpexpdate -m <mediaid> -d 0 如果不通过,看是否被其他media server写入数据.使用 nbemmcmd -listhost 查看所有media serve ...
- gitlab ce 中删除空项目之后,没有删除掉,访问500
在VirtualBox中的gitlab ce,在管理页面的操作如下: 新建一个仓库名为test的仓库,并从gitlab中导入 导入失败,使用root用户登录,在 Admin Area -> Pr ...
- Servlet & JSP - Servlet API Overview
Servlet & Generic & HttpServlet 类图 Servlet 的生命周期 init.service 和 destroy 是 servlet 的生命周期方法,它们 ...
- 第七章 jQuery操作表格及其它应用
1.表格变色 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org ...
- 把CheckedListBoxControl设置为单选框
private void chkControl_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEven ...
- windows编程socket问题
今天调试了个MFC网络程序,被bug困扰了一天,终于在收工前解决了. 大致是这样的,我们需要用上位机远程控制机器车前行.上位机上的MFC app的键盘按键响应如下:当按键按下时,系统会发送一个消息给a ...
- C# 正则表达式(一)
首先来复习一下正则表达式的基础知识,本篇文章分为2个部分,第一个部分复习正则表达式中的元字符和简写表达式,第二部分复习正则表达式的匹配和提取. 1.正则表达式中的元字符和简写表达式 ".&q ...
- java小经验
从事互联网金融,常常会碰到文件处理,以前都是傻傻的解析,这次我不想这么傻了,做个小小的封装,咱也以oop的思想来完成. 文件解析处理一般分两种模式:分隔符与定长,目前工作五年也就这两种. 封装思想: ...
- 4月10日学习笔记——jQuery选择器
概念 jQuery 是一套Javascript脚本库,注意 jQuery 是脚本库,而不是脚本框架."库"不等于"框架".jQuery 并不能帮助我们解决脚本的 ...