POJ 2739 Sum of Consecutive Prime Numbers(素数打表||深度优先搜索||暴力求解)

Sum of Consecutive Prime Numbers

Description

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 integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. 
Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2
//素数打表+暴力求解
#include<iostream>
#include<cstdio>
# define ll long long
const ll N = 10000+10;
using namespace std;
ll prime[N],num_prime=0;
ll isNotPrime[N]={1,1};//默认1为非素数
void pre()//筛选N内素数
{
    for(ll i=2;i<N;i++)
    {
        if(!isNotPrime[i])
            prime[num_prime++]=i;
        for(ll j=0;j<num_prime&&i*prime[j]<N;j++)
        {
            isNotPrime[i*prime[j]]=1;
            if(!(i%prime[j]))break;
        }
    }
}
int main()
{
    pre();
    ll tic,sum,k;
    //cout<<prime[0]<<endl;
    while(1){
        cin>>tic;
        if(tic==0)
            break;
        k=0;
        for(int i=0;i<num_prime;i++){
            sum=0;
            for(int j=i;j<num_prime;j++){
                sum+=prime[j];
                //if(sum==2)cout<<sum<<"ddd"<<endl;
                if(sum==tic){
                        //cout<<"d"<<endl;
                    k++;
                    break;
                }
                else if(sum>tic){
                    break;
                }
            }
        }
        cout<<k<<endl;
    }
}

也可以用深搜做

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define MAXN 2555550
#define mod 1000000007
#define INF 0xfffffff
#define MIN(a,b) a>b?b:a
using namespace std;
int v[MAXN];
int pri[MAXN];
int n,ans,k;
void db()
{
	int i,j;
	for(i=1;i<=10001;i++)
	v[i]=1;
	k=0;
	v[1]=0;
	for(i=2;i<=10001;i++)
	{
		if(v[i])
		{
			for(j=i*2;j<=10001;j+=i)
			{
				v[j]=0;
			}
			pri[k++]=i;
		}
	}
}
void find(int sum,int num)
{
	if(sum==n)
	{
		ans++;
		return;
	}
	if(sum+pri[num]>n)
	{
		return;
	}
	else
	{
		find(sum+pri[num],num+1);
	}
	return;
}
int main()
{
	int i;
	db();
	while(scanf("%d",&n)!=EOF,n)
	{
		ans=0;
		for(i=0;i<k;i++)
		{
			find(0,i);
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/81544674