Sum of Consecutive Prime Numbers POJ - 2739(线性欧拉筛+尺取法)

题意:

一些正整数可以由一个或多个连续质数的总和表示。给定一个的正整数n,问满足条件的有多少种情况?

题目:

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 representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.

Input

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

Output

The output should be 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 as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2

分析:

1.将 2 至 10000 内的素数存入一个数组;
2.对于每一个给定的数,从左向右遍历数组,根据连续素数的和的大小不断的增减元素,直到找到一个个解。

AC模板:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e4+10;
int n,k,r,l,ans,mi;
int dp[M],book[M];
void init()
{k=0;/** for(int i=2; i<M; i++){if(!book[i]){dp[k++]=i;for(int j=i*2; j<M; j+=i)book[j]=1;}}*/for(int i=2;i<M;i++){if(!book[i])dp[k++]=i;for(int j=0;j<k&&i*dp[j]<M;j++){book[i*dp[j]]=1;if(i%dp[j]==0)break;}}}
int solve(int x)
{ans=0;for(int i=0; i<k&&dp[i]<=x; i++){l=i,mi=0;while(mi<x&&l<k){mi+=dp[l++];}if(mi==x)ans++;}return ans;
}
int main()
{init();while(~scanf("%d",&n)&&n){printf("%d\n",solve(n));}return 0;
}

备战ccpc分站赛ing ,题目分析简略,见谅,转载请注明出处。。。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309570.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

高性能IO——Reactor模式

高性能IO——Reactor模式 参考&#xff1a;https://cloud.tencent.com/developer/article/1513447 目前的IO线程处理模型一般可以分为以下三类&#xff1a; 单线程阻塞I/O服务模型&#xff1b; while(true) {socket accept();handle(socket) }多线程阻塞I/O服务模型&#xf…

X-lab 开放实验室开源创新的故事

本报告为“开源软件供应链点亮计划暑期2020活动”中的“大咖说开源”第二期的特邀嘉宾视频&#xff0c;正好借此机会给大家介绍下 X-lab 实验室目前在开源方面开展的一些事情&#xff0c;欢迎大家关注&#xff0c;也欢迎更多热爱开源的朋友们加入&#xff01;摘要&#xff1a;2…

Circle and Points POJ - 1981(单位圆覆盖最多点)

题意&#xff1a; 给你n个点和点的位置&#xff0c;问单位圆最多能覆盖多少个点。 题目&#xff1a; You are given N points in the xy-plane. You have a circle of radius one and move it on the xy-plane, so as to enclose as many of the points as possible. Find h…

Golang 匿名函数、闭包

参考&#xff1a;https://blog.csdn.net/qq_35976351/article/details/81986496 Golang 闭包 匿名函数 Golang支持匿名函数&#xff0c;即在需要使用函数时&#xff0c;再定义函数&#xff0c;匿名函数没有函数名&#xff0c;只有函数体。 匿名函数经常被用于实现回调函数、闭…

ASP.NET Core分布式项目实战(Consent 确认逻辑实现)--学习笔记

任务22&#xff1a;Consent 确认逻辑实现接下来&#xff0c;我们会在上一节的基础上添加两个按钮&#xff0c;同意和不同意&#xff0c;点击之后会把请求 post 到 ConsentController 处理&#xff0c;如果同意会通过 return url 跳转到客户端&#xff0c;如果不同意就会取消&am…

The Last Non-zero Digit POJ - 1150(n!mod p)

题意&#xff1a; 要求你求出n!(n−m)!)\frac{n!}{(n-m)!)}(n−m)!)n!​中最后一个非0的数字. 题目&#xff1a; In this problem you will be given two decimal integer numberN,M. You will have to find the last non-zero digit of the NPM^{N}P_{M}NPM​.This means n…

Istio 1.6——迈向极简主义

从 1.2 版本开始&#xff0c;Istio 进入季度发布的节奏。5 月 21 日发布的 1.6 版本可以说是最准时的一次。我们是否可以理解 Istio 架构简化后的开发工作已经步入了正轨&#xff1f;这次的更新是否会带给我们惊喜&#xff1f;亦或是还有遗憾&#xff1f;让我们一一道来。&…

[Java基础]获取Class类的对象

代码如下: package ClassObjectPack;public class Student {private String name;int age;public String address;public Student(String name, int age, String address) {this.name name;this.age age;this.address address;}public Student() {}private Student(String …

使用PInvoke互操作,让C#和C++愉快的交互优势互补

一&#xff1a;背景1. 讲故事如果你常翻看FCL的源码&#xff0c;你会发现这里面有不少方法借助了C/C的力量让C#更快更强悍,如下所示&#xff1a;[DllImport("QCall", CharSet CharSet.Unicode)][SecurityCritical][SuppressUnmanagedCodeSecurity]private static ex…

蓝桥杯2014届试题9题 小朋友排队(树状数组+类逆序对)

题目&#xff1a; 资源限制 时间限制&#xff1a;1.0s 内存限制&#xff1a;256.0MB 问题描述 n 个小朋友站成一排。现在要把他们按身高从低到高的顺序排列&#xff0c;但是每次只能交换位置相邻的两个小朋友。 每个小朋友都有一个不高兴的程度。开始的时候&#xff0c;所有…

[Java基础]反射获取构造方法并使用

代码如下: package ClassObjectPack;import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException;public class ReflectDemo01 {public static void main(String[] args) throws ClassNotFoundExcep…

Dotnet core使用JWT认证授权最佳实践(二)

最近&#xff0c;团队的小伙伴们在做项目时&#xff0c;需要用到JWT认证。遂根据自己的经验&#xff0c;整理成了这篇文章&#xff0c;用来帮助理清JWT认证的原理和代码编写操作。第一部分&#xff1a;Dotnet core使用JWT认证授权最佳实践(一)&#xff08;接上文&#xff09;测…

题目 1886: [蓝桥杯][2017年第八届真题]包子凑数(欧几里得+完全背包)

题目&#xff1a; 时间限制: 1Sec 内存限制: 128MB 提交: 1049 解决: 365 题目描述 小明几乎每天早晨都会在一家包子铺吃早餐。他发现这家包子铺有N种蒸笼&#xff0c;其中第i种蒸笼恰好能放Ai个包子。每种蒸笼都有非常多笼&#xff0c;可以认为是无限笼。 每当有顾客想买X…

[Java基础]反射获取构造方法并使用练习

Student类代码如下: package ClassObjectPack;public class Student {private String name;int age;public String address;public Student(String name, int age, String address) {this.name name;this.age age;this.address address;}public Student() {}private Studen…

Dotnet core使用JWT认证授权最佳实践(一)

最近&#xff0c;团队的小伙伴们在做项目时&#xff0c;需要用到JWT认证。遂根据自己的经验&#xff0c;整理成了这篇文章&#xff0c;用来帮助理清JWT认证的原理和代码编写操作。一、JWTJSON Web Token (JWT)是一个开放标准(RFC 7519)&#xff0c;它定义了一种紧凑的、自包含的…

[Java基础]反射获取成员变量并使用

代码如下: package ClassObjectPack;public class Student {private String name;int age;public String address;public Student(String name, int age, String address) {this.name name;this.age age;this.address address;}public Student() {}private Student(String …

2018年蓝桥杯B组题E题+快排

题目&#xff1a; E 快速排序&#xff1a;以下代码可以从数组a[]中找出第k小的元素。 它使用了类似快速排序中的分治算法&#xff0c;期望时间复杂度是O(N)的。 请仔细阅读分析源码&#xff0c;填写划线部分缺失的内容。 #include <stdio.h> int quick_select(int a[],…

Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权

在上一讲中&#xff0c;我们已经完成了一个完整的案例&#xff0c;在这个案例中&#xff0c;我们可以通过Angular单页面应用&#xff08;SPA&#xff09;进行登录&#xff0c;然后通过后端的Ocelot API网关整合IdentityServer4完成身份认证。在本讲中&#xff0c;我们会讨论在当…