Problem D: Prime Numbers…Again!
Time Limit: 1 Sec Memory Limit: 128 MBDescription
We can write some of the numbers as the sum of some distinct consecutive prime numbers.
For instance, number 15 can be represented as 3+7+5=15
You are given a postive integer number 1<=n<=100000
and you should find the number of different ways that number n
can be represented as the sum of some distinct consecutive prime numbers.
Input
The number of test cases comes in the first line. For each test case you are given a positive integer
1<=n<=100000
Output
For each test case, print the number of the different ways that n
can be represented as a sum of some consecutive prime numbers.
Sample Input
Sample Output
#include<cstring>
#include<cstdio>
#include<iostream>
#define max 10000 //要注意这个数字,这个数字其实大小应该为100000的平方根,是根据一个数的因子小于等于这个数的平方根所得到的
using namespace std;
bool p[max]={0,0,1,1,0,1};
int prim[max]={2};
void deal()
{ int j,k=2;
memset(p,true,sizeof(p));
for(j=2;j<max;j++)
{
if(p[j])
for(k=j*j;k<max;k+=j)
p[k]=0;
}
k=0;
for(j=2;j<max;j++)
if(p[j])
prim[k++]=j;
}
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.in","w",stdout);
deal();
int t,n,sum,i,j,way;
scanf("%d",&t);
while(t--)
{
way=0;
scanf("%d",&n);
for(i=0;prim[i]<=n;i++)
{
sum=0;
for(j=i;sum<=n;j++)
{
if(sum==n)
{
way++;
break;
}
sum+=prim[j];
}
}
printf("%d\n",way);
}
return 0;
}