求连续素数和的种数(素数打表为核心算法)
Prime Numbers…Again!
Description
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
2317
Sample Output
12
#include<stdio.h>
int prim[2300]= {2,3};
int count = 0;
void prime()
{
int tally=2;
int i,j,flag;
for(i=5; i<20000; i+=2)
{
for(j=0,flag=1; prim[j]*prim[j]<=i; j++)
if(i%prim[j]==0)
flag=0;
if(flag)
{
prim[tally]=i;
tally++;
}
}
} //打表函数
int main()
{
prime();
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;
}
PS:今天收获不小,连续素数和的种数和DFS都有零的突破!