求连续素数和的种数

求连续素数和的种数(素数打表为核心算法)

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都有零的突破!







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值