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;
}




目录
相关文章
|
2月前
|
并行计算 API 开发者
Windows 环境下 llama.cpp 编译运行指南
本文详解Windows下用llama.cpp本地部署大模型:从VS2026编译(支持CPU/GPU)、ModelScope下载GGUF格式Qwen2.5-1.5B模型,到启动llama-server API服务及Python调用验证,全程轻量、低延迟、高隐私,助力开发者快速落地。
930 2
|
8月前
|
缓存 监控 数据可视化
实战指南:通过API高效获取全球股票数据分析
本文为量化交易者提供StockTV API实战指南:涵盖美股/日股数据获取、实时行情查询、多周期K线调用、技术指标计算及可视化(mplfinance),并详解WebSocket实时推送、缓存优化与容错机制,助你高效构建金融分析系统。(239字)
|
5月前
|
XML SQL Java
EmEditor 20.9.2 安装教程(emed64_20.9.2.msi 大文件文本编辑器 详细步骤)
EmEditor 20.9.2 是一款轻量高效的专业文本编辑器,支持秒开GB级大文件、语法高亮、正则替换与CSV表格编辑。本教程详解MSI安装包的管理员运行、路径设置、快捷方式创建及首次启动配置,全程图文清晰,5分钟快速上手。(238字)
|
7月前
|
人工智能 Cloud Native 算法
玄晶引擎2.9.0实操:IP自动化+爆款复刻,阿里云生态适配落地
玄晶引擎2.9.0深度适配阿里云生态,主打人设IP自动化与短视频爆款复刻两大功能,支持低代码配置、云原生部署及AI私域转化闭环。无缝对接DataWorks、MaxCompute等服务,助力企业咨询、电商、连锁等行业实现“获客—转化—复购—裂变”全流程提效降本。
570 1
|
7月前
|
缓存 前端开发 小程序
《微信商品详情页前端性能优化实战》
本文详解微信生态(公众号/小程序)商品页前端性能优化实战,直击X5内核冷启动慢、分享卡片白屏、弱网卡顿三大痛点。通过DNS预连接、微信JSSDK预加载、骨架屏水合、设备分级降级等“微信特供”方案,实现FCP降至0.9s、分享失败率从8%→0.5%,达成“秒开+零白屏”。
|
7月前
|
消息中间件 运维 监控
【消息队列MQ】消息积压:原因、紧急处理方案
本文系统解析消息积压问题,涵盖定义影响、全链路根因(90%源于消费者侧)、紧急SOP(止损→定位→消化→恢复)、长效预防、主流MQ(Kafka/RocketMQ/RabbitMQ)差异化处理、常见误区及复盘闭环七大维度,构建覆盖故障应急到架构治理的完整知识体系。
|
7月前
|
人工智能
AI写小红书和公众号为什么容易限流?问题出在你不知道的地方,分享相关真实技术经验
AI写作被限流?真相是平台打击“没人性、同质化、低情绪”的内容,而非AI工具本身。实测发现:AI辅助勿超25%–30%;同质化比AI检测更致命;每百字需1个情绪钩子。4招破局:差异化选题、建个人素材库、分段人工打磨、发布前三重检测。
1355 0
|
11月前
|
消息中间件 Prometheus 监控
百万 QPS 不是洪水猛兽:高流量服务的采样、聚合与可视化,咱得这么干!
百万 QPS 不是洪水猛兽:高流量服务的采样、聚合与可视化,咱得这么干!
510 12
|
人工智能 运维 Kubernetes
深度 | 从DevOps到BizDevOps, 研发效能提升的系统方法
研发效能提升不知从何下手、一头雾水?阿里资深技术专家一文为你揭秘研发效能提升的系统方法
5447 1
深度 | 从DevOps到BizDevOps, 研发效能提升的系统方法
|
文字识别 算法 API
阿里云文档解析(大模型版)优化
阿里云文档解析(大模型版