LC and Prime
发布时间: 2015年9月19日 21:42 时间限制: 1000ms 内存限制: 32M
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.
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).
For each test case, print the number of ways.
复制
3 9
0 2
#include<stdio.h>
#include<math.h>
int main()
{
int p3,n,sum=0;
int i,j,p[10010];
int num=10010;
p[1]=0;
for(int i=0;i<=10010;i++)
{
p[i]=i;
}
for (int i=2;i<sqrt(num);i++)
{
for(int j=i+1;j<=num;j++)
{
if(p[j]!=0&&p[j]%i==0)
{
p[j]=0;
}
}
}
while(scanf("%d",&n)!=EOF)
{
sum=0;
if(n>=6)
{
for(i=2;i<=n/3;i++)
{
if(p[i]!=0)
{
for(j=i;j+i<=(n/3)*2;j++)
{
if(p[j]!=0)
{
p3=n-i-j;
if(p3>=j&&p[p3]!=0)
{
sum++;
}
}
}
}
}
}
printf("%d\n",sum);
}
return 0;
}

本文介绍了一个算法问题:给定一个正整数n,统计有多少组素数三元组(p1,p2,p3),使得p1<=p2<=p3且p1+p2+p3等于n。通过筛法预先计算素数,并使用三层循环来寻找符合条件的素数组合。
2265

被折叠的 条评论
为什么被折叠?



