Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer
n(n≤10000).
Output
For each test case, print the number of ways.
Sample Input
3 9 给出数n,求和为n并且均为素数的p1,p2,p3(p1<=p2<=p3)的组合的个数。 后台数据是没有10000的,所以最后10000叉掉了好多人。。。
注意一下最后枚举的时候要依次进行三分和二分。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<stdio.h>
using namespace std;
int prim[10010];
int isp[10010];
int isprim(int x)
{
if(x==2||x==3)
return 1;
int i;
for(i=2;i<=sqrt((double)x);i++)
{
if(x%i==0)
return 0;
}
return 1;
}
int main()
{
int n,i,max,j,x,t,s,p;
int k=0;
for(i=0;i<=10001;i++)
isp[i]=0;
for(i=2;i<=10001;i++)
{
if(isprim(i))
{
prim[k]=i;
k++;
isp[i]=1;
}
}
while(~scanf("%d",&n))
{
x=0;
for (i=0;prim[i]<=n/3&&i<1230;i++)
{
p=(n-prim[i]);
for (j=i;prim[j]<=p/2&&j<1230;j++)
{
if(isp[p-prim[j]])
x++;
}
}
printf("%d\n",x);
}
return 0;
}