FAFU-OJ 1338 素数难题 代码

链接地址:http://acm.fafu.edu.cn/problem.php?id=1338

 

#include 
#include 
#include 
#define N  1230		//一万以内素数个数

int main()
{
	int n;
	int s[N];
	int c[10000] = {0};					//c[i]表示连续素数和为i的情况有多少种
	int a, flag;
	int i, j, x;
	int point = 2;
	int sum = 0, count;
	s[1] = 2;
	for(a = 3; a <= 10000; a += 2)
	{
		flag = 0;
		for(i = 2; i <= (int)sqrt(a)+1 && flag == 0; i++)
		{
			if(a % i == 0)
			{
				flag = 1;
			}
			if(i == (int)sqrt(a) + 1)
			{
				s[point++] = a;
			}
		}
	}

	for(i = 1; i < N; i++)
	{
		for(j = i; j < N; j++)
		{
			sum = 0;
			flag = 0;
			for(x = i; x <= j && flag == 0; x++)
			{
				sum += s[x];
				if(sum >= 10000)			//当和超过10000就不再叠加
				{
					sum -=s[x];
					flag = 1;
					break;
				}
			}
			if(flag == 0)		
				c[sum]++;
		}
	}

	while(scanf("%d", &n) == 1 && n)
	{
		printf("%d\n", c[n]);
	}

	return 0;
}



 

 

 

 

你可能感兴趣的:(FAFU,-,OJ)