POJ 2739 Sum of Consecutive Prime Numbers

POJ 2739 - Sum of Consecutive Prime Numbers 题目链接:POJ - 2739 题目概括: 正整数可以用连续的素数和来表示 计算该正整数可以有几种表示方法 正确示例: > 53 > 1. 5+7+11+13 2. 53 > 41 > 1. 2+3+5+7+11+13 2. 11+13+17 3. 41 错误示例: > 20 > 1. 7+13 不是连续的素数 > 2. 3+5+5+7 有重复 > 以上两种方式 都是无效的! 题目解析: 由于输入的整数在 [ 2, 10000 ] 的区间 阅读详情

POJ 2739 Sum of Consecutive Prime Numbers

Sum of Consecutive Prime Numbers

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 26176

Accepted: 14204

Description

Some positiveintegers can be represented by a sum of one or more consecutive prime numbers.How many such representations does a given positive integer have? For example,the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 hasonly one representation, which is 3. The integer 20 has no suchrepresentations. Note that summands must be consecutive prime 
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for theinteger 20. 
Your mission is to write a program that reports the number of representationsfor the given positive integer.

Input

The input is asequence of positive integers each in a separate line. The integers are between2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output shouldbe composed of lines each corresponding to an input line except the last zero.An output line includes the number of representations for the input integer asthe sum of one or more consecutive prime numbers. No other characters should beinserted in the output.

Sample Input

2

3

17

41

20

666

12

53

0

Sample Output

1

1

2

3

0

0

1

2

 

 

先生成素数表,根据素数表求连续素数的和,最后根据输入查询即可

 

 

代码:

//POJ 2739 Sum of COnsecutive Prime Numbers

#define LOCAL

#include <iostream>

#include <cstring>

#include <vector>

using namespace std;

const int MAX = 10050;

bool isprime[MAX];

int prime[MAX];

int top;

int counter[MAX];

void initPrimeArray(){

    top = 1;

    memset(isprime,true,sizeof prime);

    for(int i=2;i<MAX;i++)

        if(isprime[i]){

            prime[top++]= i;

            for(int j=i*2;j<MAX;j+=i)

                isprime[j]= false;

        }

}

 

void initCounter(){

    std::vector<int> sum(top,0);

    memset(counter,0,sizeof counter);

    for(int i=1;i<top;i++){

        sum[i]= sum[i-1]+ prime[i];

        for(int j=0;j<i;j++)

            if(sum[i]-sum[j]<MAX)

                counter[sum[i]-sum[j]]++;

    }

}

 

int main(int argc,char const *argv[])

{

    initPrimeArray();

    initCounter();

    #ifdef LOCAL

    freopen("in.txt","r",stdin);

    #endif

    int n;

    while(cin>>n&&n){

        cout<<counter[n]<<endl;

    }

    return 0;

}

 

POJ2739-Sum of Consecutive Prime Numbers 北大POJ2739-Sum of Consecutive Prime Numbers 解题报告+AC代码 立即下载

相关推荐

poj 第九季 第十季

Dexter need help;Checksum algorithm;Graveyard;Hanoi II;Houseboat;Sum of Consecutive;Arithmetic Progressions;Redistribute wealth;Scoring;Specialized Numbers;操场训练;最长回文子串;寻找规律;公园喷水器;阶乘合计;不屈的小蜗;Hailstone;The Ratio of gainers to losers

R7-33 First 50 Prime Numbers(JAVA)

Your program reads one natural numbers n in, and prints out the sum of the first n prime numbers starting from 2.A positive whole numbers n, which is less than 103.A number which is the sum of all the first n prime numbers. Sample Output:

qq_52331221的博客 504

西工大poj 100道经典c语言习题答案

西工大poj 100道经典c语言习题答案 有详细解答

7-24 First 50 Prime Numbers (10 分)

7-24 First 50 Prime Numbers Your program reads one natural numbers n in, and prints out the sum of the first n prime numbers starting from 2. 输入格式: A positive whole numbers n, which is less than 10 ​3 ​​ . 输出格式: A number which is the sum of all the first

balding_man的博客 1377

yolov5 onnx推理时报错[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type.

yolov5 onnxruntime 推理报错

zjulkw的博客 1222

48V电气架构全面科普和解析:下一代智能电动汽车核心驱动

随着电动汽车(EV)技术的不断发展,传统的12V电力系统已经逐渐无法满足日益增长的功率需求,尤其是在电动汽车中,48V电气系统的引入成为了一种理想的解决方案。在电动汽车中,48V系统与Zonal架构的结合,不仅有效提升了车辆电力系统的能效,还为未来的技术升级提供了灵活的设计基础。随着电动汽车(EV)技术的发展,车辆电气系统的工作电压逐步从传统的12V系统向更高的电压过渡。随着电动汽车技术的不断进步,传统的12V电气系统逐渐无法满足现代高性能电动汽车的需求,而48V电力系统的引入成为一种理想的解决方案。

weixin_43199439的博客 7810

1259. Sum of Consecutive Primes(连续素数求和)

/* 1259. Sum of Consecutive Primes 题目大意:给出一个数n,求出n可以用连续素数和表示的总数 如41 = 2+3+5+7+11+13或11+13+17或41,有3总 思路:先用筛法求出一个大范围内的素数数组,然后在该数组 中进行傻逼式的试和.... */ #include #includ

xiehaoyun2012的专栏 634

HCLR-Net: 混合对比学习正则化与局部随机扰动用于水下图像增强

独特贡献在于开发了一种处理负样本的新策略,既保留了负样本的统计特性,又防止网络过度剖析这些样本中的特征信息。通过根据每个通道特征的重要性对模型进行加权,我们可以增强富含上下文信息的通道,从而获得更准确、更详细的特征表示。AHA 模块利用垂直方向上的 Avgpool 和水平方向上的 Maxpool 来捕捉远程依赖关系和准确的位置信息,从而有效确定不同方向上的特征退化。对比学习策略通常旨在最小化正样本之间的欧氏距离,使预测图像更接近理想图像,同时最大化负样本与预测图像之间的距离,使它们进一步远离非理想表征。

G-Howe的博客 1457

Sum of Consecutive Prime Numbers POJ - 2739(尺取)

简单的尺取题, 拿来练手咯, 顺便复习一波欧拉筛 #include &amp;lt;cstdio&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;algorithm&amp;gt; #include &amp;lt;cstring&amp;gt; using namespace std; #define ms(x, n) memset(x,n,sizeof(x)); type

Suprit's blog 495

某AI推荐系统项目管理复盘:架构师总结的6大方法论与调优策略

我是张三,某大厂资深AI架构师,主导过5个千万级用户的推荐系统项目(覆盖电商、内容、视频、音乐、外卖等场景)。我的公众号“AI架构师笔记”会分享更多AI项目管理和技术实战经验,欢迎关注。最后的话:推荐系统不是“一锤子买卖”,而是“持续优化的过程”。希望这篇文章能帮你少踩坑,多做对——毕竟,把复杂的事做成,才是架构师的核心能力。

2501_92132293的博客 362

【AI面试】 Python 的装饰器

Python装饰器是一种高阶函数,用于在不修改原函数代码的情况下增强其功能。它通过@语法糖接收目标函数作为参数,并返回一个包含额外逻辑(如日志、缓存、权限验证等)的新函数。装饰器的核心价值在于代码复用和解耦,符合开闭原则,典型应用包括性能分析、权限控制等场景。例如,@timer_decorator可自动为函数添加耗时统计功能。本质上,@decorator等价于func = decorator(func),实际调用的是装饰器返回的包装函数。进阶技巧包括使用@functools.wraps保留原函数元信息。装饰

钱多多先森 183

POJ - 2739 Sum of Consecutive Prime Numbers

POJ 2739Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has t...

RS的博客 290

制作你自己的yolov5数据集并进行训练

项目地址:make-your-yolov5_dataset 一、数据集标注软件 labelImg labelMe 精灵标注助手 更多的标注工具你可以去看:深度学习图像标注工具汇总 、 十个最常用深度学习图像/视频数据标注工具 、 百度一下 在这里我选择使用labelImg 二、数据集标注 下载labelImg 解压,直接打开labelimg.exe labelImg标注数据 标注完成后即可获得VOC格式(生成.xml文件)的数据 目录结构: dataset │ ├─Annotations .

年少的勇气已经用完,剩下的就是三思而后行 1万+

POJ-2739 Sum of Consecutive Prime Numbers

POJ-2739 Sum of Consecutive Prime Numbers 题目链接:POJ-2739 题目大意:某些数组可以表示为连续的素数之和,问你给定一个数,有多少种素数之和的表示形式 解题思路:对范围内的素数进行存储,之后向下搜索和为给定数就sum++ 代码块: #include<iostream> #include<cmath> #include<...

鱼头的水题之路 356

python实现带符号的二进制数转换十进制数 (附完整源码)

python实现带符号的二进制数转换十进制数 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 554

RT-DETR改进策略【卷积层】| CVPR2025 GBConv轻量门控瓶颈卷积 低秩瓶颈降参 + 门控聚焦形态,强效捕捉纤细目标

裂缝形态不规则、纹理纤细,传统卷积参数大、计算冗余,且无法动态聚焦裂缝形态;背景噪声强易干扰特征提取,需要轻量且能自适应聚焦裂缝形态的卷积单元,因此提出轻量门控瓶颈卷积 GBC。

Limiiiing的博客 140

POJ-2739-Sum of Consecutive Prime Numbers

POJ-2739-Sum of Consecutive Prime Numbers http://poj.org/problem?id=2739 判断一个数能由多少种连续的素数构成,数目不大,先打表求出范围内的所有素数 #include #include #include #define N 10005 char a[N]; int b[N]; int main() { int i,j,t

菜鸟的程序人生 6174

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Poj 2739:Sum of Consecutive Prime Numbers

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two repre...

weixin_37992828的博客 310

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

POJ 2739 Sum of Consecutive Prime Numbers

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19035   Accepted: 10435 Description Some positive integers can be represente

笨鸟 965

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

poj2739 Sum of Consecutive Prime Numbers

线性筛+枚举

YHaX的博客 316
上一篇: POJ 2159 Ancient Cipher
下一篇: POJ – 1503 Integer Inquiry Integer Inquiry
朔北冥
博客等级 码龄11年 14粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值