Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

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

思路:直接线性筛 筛素数 暴力找就行   (素数到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

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 bool Is_Primes[10005];
 5 int Primes[10005];
 6 int cnt;
 7 void Prime(int n){
 8      cnt=0;
 9      memset(Is_Primes,0,sizeof(Is_Primes));
10     for(int i=2;i<=n;i++){
11         if(!Is_Primes[i])
12             Primes[cnt++]=i;
13         for(int j=0;j<cnt&&i*Primes[j]<n;j++){
14             Is_Primes[Primes[j]*i]=1;
15             if(i%Primes[j]==0)break;
16         }
17     }
18 }
19 int main(){
20     int n;
21     Prime(10003);
22     while(scanf("%d",&n)==1&&n){
23         int ans=0;
24         for(int i=0;i<cnt&&Primes[i]<=n;i++){
25             int temp=i;
26             int sum=0;
27             while(sum<n&&temp<cnt){
28                 sum+=Primes[temp++];
29             }
30             if(sum==n)ans++;
31         }
32         int flag=0;
33
34         printf("%d\n",ans);
35     }
36     return 0;
37 }

原文地址:https://www.cnblogs.com/ttttttttrx/p/10279595.html

时间: 2024-09-30 14:05:30

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

Sum of Consecutive Prime Numbers POJ - 2739

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 intege

Greedy:Sum of Consecutive Prime Numbers(POJ 2739)

 素数之和 题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数 方法:打表,然后用游标卡尺法即可 #include <iostream> #include <functional> #include <algorithm> #define MAX_N 10010 using namespace std; static int primes_set[MAX_N], flag[MAX_N], p_sum; void in

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

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

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2739 Sum of Consecutive Prime Numbers 尺取法

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21924   Accepted: 11996 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

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

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20020   Accepted: 10966 Description Some positive integers can be represented by a sum of one or more consecutive

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

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2379 Sum of Consecutive Prime Numbers

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24498   Accepted: 13326 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj2739 Sum of Consecutive Prime Numbers (素数打表)

D - Sum of Consecutive Prime Numbers Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2739 Appoint description: System Crawler (2016-05-05) Description Some