您的位置:首页 > 其它

UVa1210 Sum of Consecutive Prime Numbers

2014-07-30 10:39 393 查看
题意:求一个数能表示为几组连续素数相加的形式。如41=2+3+5+7+11+13=11+13+17=41,结果就是3。

思路:筛法素数打表,暴力枚举连续素数的起点和终点统计。

#include <iostream>  
#include <stdio.h>  
#include <cmath>  
#include <algorithm>  
#include <iomanip>  
#include <cstdlib>  
#include <string>  
#include <memory.h>  
#include <vector>  
#include <queue>  
#include <stack>  
#include <map>
#include <set>
#include <ctype.h>  
#include<time.h>
#define INF 1000000

using namespace std;

bool tab[10010];
int primes[2000];
int ans[10010];

int main(){
	for(int i=0;i<10010;i++)tab[i]=1;
	tab[0]=tab[1]=0;
	for(int i=2;i<=5000;i++){
		for(int j=i*i;j<=10000;j+=i){
			tab[j]=0;
		}
	}
	int count=1;
	primes[1]=2;
	for(int i=3;i<=10000;i+=2){
		if(tab[i])primes[++count]=i;
	}
	
	memset(ans,0,sizeof(ans));
	for(int i=1;i<=count;i++){
		int tmp=0;
		for(int j=i;j<=count;j++){
			tmp+=primes[j];
			if(tmp<=10000)ans[tmp]++;
		}
	}
	
	int n;
	while(cin>>n){
		if(!n)break;
		cout<<ans
<<endl;
	}
	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: