POJ-2739-Sum of Consecutive Prime Numbers
2013-08-06 17:35
351 查看
这个题要求求出一个素数能够有多少种连续的素数和表示。
思路:
1、先筛选出素数。
2、维护一个队列,进行更新即可
代码:
思路:
1、先筛选出素数。
2、维护一个队列,进行更新即可
代码:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int maxn=10001; int n,pri[maxn],cnt=0; bool ispri[maxn]; void Init() { ispri[1]=1; cnt=0; for(int i=2;i<maxn;i++) { if(!ispri[i]) pri[cnt++]=i; for(int j=2;i*j<maxn;j++) ispri[i*j]=1; } } int main() { Init(); while(scanf("%d",&n)&&n) { int ans=0,sum=0,front=0,last=0; while(front<=last&&last<cnt) { if(pri[last]>n) break; sum+=pri[last++]; while(sum>n) sum-=pri[front++]; if(sum==n) ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 【POJ-2739】Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers(尺取,暴力)
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers 素数打表+尺取法
- POJ 2739 Sum of Consecutive Prime Numbers(尺取)
- POJ-2739 Sum of Consecutive Prime Numbers
- Poj - 2739 - Sum of Consecutive Prime Numbers【尺取法+筛子】
- POJ 2739 Sum of Consecutive Prime Numbers 数论
- Poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers(尺取+筛素数)
- POJ-2739 Sum of Consecutive Prime Numbers-求一个数满足有多少组递增相加的素数等于该数
- POJ 2739 Sum of Consecutive Prime Numbers(数论)
- poj - 2739 - Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- (尺取法+素数打表)poj2739,Sum of Consecutive Prime Numbers
- 【原】 POJ 2739 Sum of Consecutive Prime Numbers 筛素数+积累数组 解题报告
- POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
- poj 2739 Sum of Consecutive Prime Numbers
- 【POJ】2739 - Sum of Consecutive Prime Numbers 尺取法+素数