POJ 2739 Sum of Consecutive Prime Numbers
2012-02-08 22:41
459 查看
题意:给你一个数,,求有几种方法可以用连续的素数相加的和而成。。直接打个素数表,然后DFS。。简单题。。
#include <iostream>
using namespace std;
int p[1250],pnum;
int n;
bool u[10006];
void getprim()//素数表。。
{
int i,j;
int k,x,m=(10003+1)>>1;
p[0]=2,p[1]=3;pnum=2;
for(i=3;i<=m;i+=3)
for(j=0;j<2;j++)
{
x=2*(i+j)-1;
while(u[x]==0)
{
p[pnum++]=x;
for(k=x;k<=10003;k+=x)
u[k]=1;
}
}
}
int dfs(int i,int y)
{
if(y==n)
return 1;
for(int j=i;p[j]<=n;j++)
{
y+=p[j];
if(y==n)
return 1;
else
if(y>n)
return 0;
}
return 0;
}
int main()
{
int i;
int count;
while(cin>>n)
{
if(n==0)
break;
getprim();
count=0;
for(i=0;p[i]<=n;i++)
{
if(dfs(i+1,p[i]))
count++;
}
cout<<count<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int p[1250],pnum;
int n;
bool u[10006];
void getprim()//素数表。。
{
int i,j;
int k,x,m=(10003+1)>>1;
p[0]=2,p[1]=3;pnum=2;
for(i=3;i<=m;i+=3)
for(j=0;j<2;j++)
{
x=2*(i+j)-1;
while(u[x]==0)
{
p[pnum++]=x;
for(k=x;k<=10003;k+=x)
u[k]=1;
}
}
}
int dfs(int i,int y)
{
if(y==n)
return 1;
for(int j=i;p[j]<=n;j++)
{
y+=p[j];
if(y==n)
return 1;
else
if(y>n)
return 0;
}
return 0;
}
int main()
{
int i;
int count;
while(cin>>n)
{
if(n==0)
break;
getprim();
count=0;
for(i=0;p[i]<=n;i++)
{
if(dfs(i+1,p[i]))
count++;
}
cout<<count<<endl;
}
return 0;
}
相关文章推荐
- 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
- LA-3399 & 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
- POJ 2739 Sum of Consecutive Prime Numbers(素数)