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< ...
随机推荐
- php-fpm配置文件详解
第一部分:FPM 配置 参数 | 说明 -p | 命令行中动态修改--prefix ;include=etc/fpm.d/*.conf | 用于包含一个或多个文件,如果glob(3)存在(glob() ...
- jquery点击控制动画暂停开始
一下是从w3c上面考下来了的, animation:[[ animation-name ] || [ animation-duration ] || [ animation-timing-functi ...
- CSS组件化思考
为什么组件化? 分层设计,代码复用,减少冗余: 维护方便,弹性好: 如何组件化? 目前代码分成三级: 第一级粒度最细,是基础,主要包含字体配置,颜色配置,UI框架(比如MUI或者pure.css): ...
- Bootstrap之Footer页尾布局(2015年05月28日)
直接上页尾部分的代码: <!--采用container-fluid,使得整个页尾的宽度为100%,并设置它的背景色--><footer class="container-f ...
- Ajax具体使用
Ajax在注册页面经常使用,当注册用户是用户名必须唯一时,要使用ajax获取表单中你输入的内容, 然后将这个数据利用XMLHttpRequest对象请求一个php文件,这个php文件接收到信息,查询数 ...
- C#算法基础之堆排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- EF4.1之Code first 的几种连接数据库的方式
通过代码 进行连接和创建数据库的方法主要分为两种: 1.使用用连接字符串(在配置文件里面): 连接字符串: <add name="DbEntities" connection ...
- Office升级到2013版后无法登录微软账号问题
自打office从2010版升级到2013版,就再也无法登录微软账号了.每次点击登录,弹出来的框就显示:this feature has been disabled by your administr ...
- iOS - 视图与手势(UIview & UIGestureRecognizer)
01 UIView视图的基本使用 --- 在根视图中添加2个UIView视图 //视图确实加载时调用 - (void)viewDidLoad { [super viewDidLoad]; // Do ...
- (转)Hessian(C#)介绍及使用说明
什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...