记录编号 331902 评测结果 AAAAAAAAAA
题目名称 [Japan2005] 连续素数和 最终得分 100
用户昵称 GravatarJustpenz233 是否通过 通过
代码语言 C++ 运行时间 0.038 s
提交时间 2016-10-28 09:28:52 内存使用 0.40 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10000 + 10;
bool flag[maxn] = {0};
int cnt1[maxn] = {0};
int prime[maxn];
int cnt;
int n;
inline int read()
{
	char ch;bool flag = false;int a = 0;
	while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
	if (ch != '-')
	{
		a *= 10;
		a += ch - '0';
	}
	else
		flag = true;
	while (((ch = getchar()) >= '0') && (ch <= '9'))
	{
		a *= 10;
		a += ch - '0';
	}
	if (flag)
		a = -a;
	return a;
}
void write(int a)
{
	if (a >= 10)
		write(a / 10);
	putchar(a % 10 + '0');
	putchar('\n');
}
void get_prime()
{
	cnt = 0;
	for (int i = 2; i <= 10000; i++)
	{
		if (!flag[i])
			prime[++cnt] = i;
		for (int j = 1; j <= cnt, prime[j]*i <= 10000; j++)
		{
			flag[i * prime[j]] = true;
			if (i % prime[j] == 0)
				break;
		}
	}
}
int main()
{
	freopen("conprime.in", "r", stdin);
	freopen("conprime.out", "w", stdout);
	get_prime();
	for (int i = 1; i <= cnt; i++)
	{
		cnt1[prime[i]]++;
		int ans = prime[i];
		for (int j = i + 1; j <= cnt; j++)
		{
			ans += prime[j];
			if (ans > 10000)
				break;
			cnt1[ans]++;
		}
	}
	n = read();
	while (n)
	{
		write(cnt1[n]);
		n = read();
	}
	fclose(stdin); fclose(stdout);
}