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< ...
随机推荐
- 你所不知道的z-index的用法
在开始今天的内容之前,先让我们来看下面一段代码: <style type="text/css"> #div1,#div2{ width:200px; height:20 ...
- Linux 命令 - killall: 通过进程名向进程发送信号
命令格式 killall [-Z CONTEXT] [-u USER] [ -eIgiqrvw ] [ -SIGNAL ] NAME... killall -l, --list killall -V, ...
- 在Ubuntu系统中解压rar和zip文件的方法
大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...
- Cocos2d-x中创建SQLite数据库
我们下边介绍如何通过SQLite3提供的API实现MyNotes数据库创建.创建数据库一般需要经过如下三个步骤.(1) 使用sqlite3_open函数打开数据库.(2) 使用sqlite3_exec ...
- Foundation与Core Foundation内存管理基本原则简述
内存管理是一个十分重要的事情,稍有不慎就会发生内存泄漏或者是野指针的错误.内存泄漏一般表示没有任何指针指向的内存区域,由于这块内存在对象图中无法查找到,所以有可能永远都无法回收,如果内存泄漏的空间比较 ...
- c# DataGridView操作
#region 操作DataGridView /// <summary> /// 初始化DataGridView属性 /// </summary> /// <param ...
- swift基础知识学习
用let来声明常量,用var来声明变量 声明变量: var x = 0.0, y = 0.0, z = 0.0 var welcomeMessage: String 常量和变量的命名 你可以用任何 ...
- css3学习笔记之用户界面
CSS3 调整尺寸(Resizing) CSS3中,resize属性指定一个元素是否应该由用户去调整大小. 这个 div 元素由用户调整大小. (在 Firefox 4+, Chrome, 和 Saf ...
- 【转载】应广大群众的要求,今天开始连载《超容易的Linux系统管理入门书》一书
学习Linux容易嘛?我说超容易,你肯定不信.那学习Linux最好的学习方法是什么,就是脑子里面一直提问题,不停的提,时时刻刻提,如果你没有问题,那再容易的学习书你也看不懂. <超容易的Linu ...
- Ubuntu将软件(Sublime Text 2为例)锁定到启动器
Ubuntu中打开某安装好的软件,然后右击启动器(Launcher)上打开的图标就可以将该软件锁定到启动器或者从启动器解锁. 然而,有许多软件下载后直接解压就能用,不需要安装,这种情况采用上述方法锁定 ...