题意:给一个数 可以写出多少种  连续素数的合

思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数)

其中:线性筛的证明参考:https://blog.csdn.net/nk_test/article/details/46242401

            https://blog.csdn.net/qq_40873884/article/details/79124552

            https://blog.csdn.net/baoli1008/article/details/50788512

线性筛的思想 就是 每个数都有一个最小的质因素  外层i是质因数个数 内层j是primes[i]的标号  用每个质因数筛 每个数只要被筛一遍

      同时, if(i%primes[j]==0 )break; 这里指的是如果i%primes[j]==0了 那么i就有因数 primes[j] 所以i*prime[j+1]肯定已经被筛掉了

因为是从小到大开始筛的,i中还有primes[j] 说明 i*primes[j+1]最小的质因数等于primes[j]  所以肯定会被筛掉 只是可能循环轮次

      不同,可能是下一个i或者其他i

 #include<cstdio>
#include<cstring>
using namespace std;
bool Is_Primes[];
int Primes[];
int cnt;
void Prime(int n){
cnt=;
memset(Is_Primes,,sizeof(Is_Primes));
for(int i=;i<=n;i++){
if(!Is_Primes[i])
Primes[cnt++]=i;
for(int j=;j<cnt&&i*Primes[j]<n;j++){
Is_Primes[Primes[j]*i]=;
if(i%Primes[j]==)break;
}
}
}
int main(){
int n;
Prime();
while(scanf("%d",&n)==&&n){
int ans=;
for(int i=;i<cnt&&Primes[i]<=n;i++){
int temp=i;
int sum=;
while(sum<n&&temp<cnt){
sum+=Primes[temp++];
}
if(sum==n)ans++;
}
int flag=; printf("%d\n",ans);
}
return ;
}

Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)的更多相关文章

  1. Greedy:Sum of Consecutive Prime Numbers(POJ 2739)

     素数之和 题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数 方法:打表,然后用游标卡尺法即可 #include <iostream> ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  4. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  5. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  6. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  8. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  9. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

随机推荐

  1. C++ 通过ostringstream 实现任意类型转string

    #include <iostream> #include <string> using namespace std; int main() { ; double b = 65. ...

  2. 牛客网 Python 编程输入规范

    import sys try: while True: line = sys.stdin.readline().strip() if line == '': break lines = line.sp ...

  3. Python_装饰器习题_31

    # 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), # 要求登录成功一次,后续的函数都无需再输入用户名和密码 FLAG = False def login(func): def ...

  4. Generalized Power Method for Sparse Principal Component Analysis

    目录 重点 算法 这篇文章,看的晕晕的,但是被引用了400多次了,就简单地记一笔. 这个东西,因为\(\ell_1\)范数,所以会稀疏化,当然,和\(\gamma\)有关. 重点 我想重点写的地方是下 ...

  5. c++入门之再次探讨类属性

    精辟博文:https://blog.csdn.net/msdnwolaile/article/details/51923859(转载,仅供学习|!)

  6. Navicat还原出现Finished - Stopped before completion的问题

    查看数据库中最大的单个文件容量 SHOW VARIABLES LIKE '%max_allowed_packet%';   设置最大单个文件容量为10M,单次有效(新建查询---运行) SET GLO ...

  7. 无法从带有索引像素格式的图像创建graphics对象

    大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...

  8. Servlet 使用ServletContext共享数据,读取web.xml配置

    ServletContext对象 session和cookie,对于每一个请求用户来说,都是不同的,因为要保证隐私安全. 而有一些数据,可以让所有用户共享,此时就可以用ServletContext对象 ...

  9. Linux 环境变量梳理

    Linux中的环境变量有两种:全局变量和局部变量: 定义.访问.删除局部变量 查看全局变量 可以使用printenv或者env命令来打印所有的全局变量. 访问某一项全局变量,可以使用printenv ...

  10. [转帖]SUSE Linux

    历经坎坷多次易主,SUSE Linux路在何方? http://blog.itpub.net/11310314/viewspace-2638811/ 之前一直理不清楚 SUSE和RedHat的关系 甚 ...