LightOJ 1259 Goldbach`s Conjecture 素数打表

题目大意:求讲一个整数n分解为两个素数的方案数。

题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bool类型可以节约不少内存。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#define INF 0x3f3f3f3f
#define MAX 10000005
#define Temp 10000005

using namespace std;

int p[664589],cnt=0;
bool vis[MAX];

void GetPrime()//素数打表
{
    memset(vis,false,sizeof(vis));
    memset(p,0,sizeof(p));
    for(int i=2;i<MAX;i++)
    {
        if(!vis[i])
        {
            p[++cnt]=i;
            for(int j=i+i;j<MAX;j+=i)
            {
                vis[j]=true;
            }
        }
    }
}

int main()
{
    GetPrime();
    int n,T,sum,cns=0;
    scanf("%d",&T);
    while(T--)
    {
        sum=0;
        scanf("%d",&n);
        for(int i=1;p[i]<=n/2;i++)//遍历寻找方案数
        {
            if(vis[n-p[i]]==false)
                sum++;
        }
        printf("Case %d: %d\n",++cns,sum);
    }
    return 0;
}

时间: 2024-08-29 15:53:25

LightOJ 1259 Goldbach`s Conjecture 素数打表的相关文章

LightOJ 1259 Goldbach`s Conjecture 水题

不想说了 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std; typedef long

POJ 2262 Goldbach&#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.如果存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的所有素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,如果存在素数a+素数b

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&amp;#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.假设存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的全部素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,假设存在素数a+素数b

Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质数 ... 我真是蠢啊 还有  vis要用bool类型的!!!!  int会直接爆 代码如下: #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #includ

HDU 1397 Goldbach&#39;s Conjecture(素数打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1397 Problem Description 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

HDU 1397 Goldbach&#39;s Conjecture【素数打表】

题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<algorithm> 9 #define mod=

POJ 2262 Goldbach&#39;s Conjecture(素数筛选法)

Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: Every even number greater than 4 can be written as the sum of two odd prime numbers. For example: 8 =

POJ 2262 Goldbach&#39;s Conjecture (求解素数的一般筛和线性筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40944   Accepted: 15664 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject