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

题意:

就是哥德巴赫猜想。。。任意一个偶数 都可以分解成两个(就是一对啦)质数的加和

输入一个偶数求有几对。。

解析:

首先! 素数打表。。因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质数 。。。 我真是蠢啊

还有  vis要用bool类型的!!!!  int会直接爆

代码如下:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 10000000
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f;
LL primes[700000];
bool vis[maxn];
int cnt = 0;
void init()
{
    mem(vis,0);
    primes[1] = 1; primes[0] = 1;
    for(int i=2; i<maxn; i++)
        if(!vis[i])
        {
            primes[cnt++] = i;
            for(LL j=(LL)i*i; j<maxn; j+=i)
                vis[j] = 1;
        }
}

int main()
{
    int T;
    init();
    int res = 0;
    cin>> T;
    while(T--)
    {
        int ans = 0;
        int n;
        cin>> n;
        for(int i=0; i<cnt && primes[i] <= n/2; i++)
        {
            if(!vis[n-primes[i]])
                ans++;
        }
        printf("Case %d: %d\n",++res,ans);

    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9184259.html

时间: 2024-08-13 14:36:54

Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)的相关文章

Lightoj 1370 素数打表 +二分

1370 - Bi-shoe and Phi-shoe   PDF (English) Statistics   Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for hi

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&g

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(素数相关)

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(素数筛选法)

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: 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&#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

LightOJ Bi-shoe and Phi-shoe 1370【欧拉函数+素数打表】

1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for