HDU 2098 分拆素数和 数论
分拆素数和
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
把一个偶数拆成两个不同素数的和,有几种拆法呢?
Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
Sample Input
30
26
0
Sample Output
3
2
关键是做一下预处理
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
using namespace std;
#define FIN freopen("input.txt","r",stdin);
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
int p[10005];
int s[5005];
int main()
{
//FIN
int cas=0;
for(int i=2; i<=10000; i++)
{
if(p[i]==0)
{
for(int j=i+i; j<=10000; j=j+i)
{
p[j]=1;
}
}
}
for(int i=2; i<=10000; i++)
{
if(p[i]==0)
s[cas++]=i;
}
int n;
while(~scanf("%d",&n)&&n)
{
int cnt=0;
for(int i=3; i<n/2; i+=2)
{
if(!p[i]&&!p[n-i])
cnt++;
}
printf("%d\n",cnt);
}
}

浙公网安备 33010602011771号