Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

素数筛法

Posted by 422245257 at 2019-08-30 17:00:27 on Problem 2739
#include<cstdio>
#include<cstring>
using namespace std;
int prime[5000];
int cnt;
bool mark[10005];
int number[10005];
int sum[10005];
int main(){
	int i,j,k,n;
	cnt=1;
	memset(mark,false,sizeof(mark));
	memset(number,0,sizeof(number));
	for(i=2;i<=10000;i++){
	if(!mark[i]){
		prime[cnt++]=i;
	   }
		for(j=1;j<cnt&&i*prime[j]<=10000;j++)
		mark[i*prime[j]]=true;
    }
    memset(sum,0,sizeof(sum));
    for(i=1;i<cnt;i++)
    sum[i]+=sum[i-1]+prime[i];
    for(i=1;i<cnt;i++){
    	for(j=i;j<cnt;j++){
    		sum[j]-=sum[i-1];
    		if(sum[j]<=10000) number[sum[j]]++;
		}
	}
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		printf("%d\n",number[n]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator