LIGHT OJ 1259 - Goldbach`s Conjecture(素数筛选)

简介:

传送门


1259 - Goldbach`s Conjecture
Time Limit: 2 second(s) Memory Limit: 32 MB

Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

Every even integer, greater than 2, can be expressed as the sum of two primes [1].

Now your task is to check whether this conjecture holds for integers up to 107.

Input

Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).

Output

For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b)where

1)      Both a and b are prime

2)      a + b = n

3)      a ≤ b

Sample Input

Output for Sample Input

2

6

4

Case 1: 1

Case 2: 1

Note

1.      An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...



题目大意:

首先给定T组数据,让你求的是,一个>=4的偶数拆分成两个素数之和的形式能够有几对。


样例解释:

当 n = 6时,6 = 3 + 3只有一对,

当 n=10时,10 = 5 + 5 = 3 + 7(7+3)有两对所以输出3


解题思路:

就是一个素数筛,没什么说的感觉(我还错了几次),当时也不知道是怎么回事,有可能是编译器坏了,明明一样的代码,在自己这就是不能过,但是交上去就能过,真是神了,需要注意的是素数筛的时候要记录素数的值,在主函数中用到,不能直接 n/2算,会超时,最后因为我们算的是对数所以要除以2,然后再判断 n/2是不是素数,如果是+1

不是不用变,上代码:


My Code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 1e7+5;
const int M = 1e6+5;
typedef long long LL;
int p[M];
bool prime[MAXN];
int cnt = 0;
void isprime()
{
    cnt = 0;
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = false;
    for(LL i=2; i<MAXN; i++)
    {
        if(prime[i])
        {
            p[cnt++] = i;
            for(LL j=i*i; j<MAXN; j+=i)
                prime[j] = false;
        }
    }
}
int main()
{
    isprime();
    int T,n;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        scanf("%d",&n);
        int sum = 0;
        for(int i=0; i<cnt&&p[i]<=n; i++)
            if(prime[n-p[i]])
                sum++;
        sum >>= 1;
        if(prime[n/2])
            sum++;
        printf("Case %d: %d\n",cas,sum);
    }
    return 0;
}




目录
相关文章
|
运维 负载均衡 应用服务中间件
LVS详解(五)——LVS NAT模式实战
LVS详解(五)——LVS NAT模式实战
323 3
|
Windows 安全
远程桌面和云主机,可以听到云主机内部的声音
有些云主机用户,在使用云主机的时候想借助云主机的高带宽听音乐或者看视频的,那么如何设置远程桌面和云主机,可以听到云主机内部的声音?下面小编就来个图文教程,希望能帮助到这些客户使用云主机。   本地计算机需要进行的设置: 1、打开本地机器的远程登录功能,这里介绍一种快捷的方式。
2766 0
|
8月前
|
Arthas 监控 Java
Arthas logger(查看 logger 信息,更新 logger level)
Arthas logger(查看 logger 信息,更新 logger level)
298 6
|
SQL JSON 关系型数据库
MySQL是一个广泛使用的开源关系型数据库管理系统,它有许多不同的版本
【10月更文挑战第3天】MySQL是一个广泛使用的开源关系型数据库管理系统,它有许多不同的版本
663 5
|
消息中间件 存储 监控
RocketMQ的性能优势?
【8月更文挑战第29天】RocketMQ的性能优势?
483 2
|
分布式计算 大数据 Hadoop
MapReduce:大数据处理的基石
【8月更文挑战第31天】
556 0
|
敏捷开发 Java API
阿里云云效产品使用合集之如何使用JDK 21进行打包
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
安全 程序员 数据库
程序员必知:xadmin快速搭建后台管理系统
程序员必知:xadmin快速搭建后台管理系统
482 0
|
SQL Web App开发 安全
【Less-9】基于SQLI的SQL时间盲注
【Less-9】基于SQLI的SQL时间盲注
|
Cloud Native 网络协议 Java
基于Spring Cloud Alibaba的微服务架构实战:Nacos配置管理
基于Spring Cloud Alibaba的微服务架构实战:Nacos配置管理
834 0