HDU 5104 Primes Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104


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(n10000) .
 
Output
For each test case, print the number of ways.

Sample Input
   
   
   
   
3 9

Sample Output
   
   
   
   
0 2

Source
BestCoder Round #18 

代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
int p[10017];
void init()
{
    memset(p,0,sizeof(p));
    for(int i = 2; i <= 10000; i++)
    {
        if(p[i] == 0)
        {
            for(int j = i*i; j <= 10000; j+=i)
            {
                p[j] = 1;
            }
        }
    }
}
int main()
{
    int a[10017];
    int n;
    init();
    while(~scanf("%d",&n))
    {
        int cont = 0;
        for(int i = 2; i <= n; i++)
        {
            if(p[i] == 0)
                for(int j = i; j <= n; j++)
                {
                    if(p[j] == 0 && p[n-i-j] == 0)
                        if(n-i-j>=i && n-i-j>=j)
                        {
                            cont++;
                        }
                }
        }
        printf("%d\n",cont);
    }
    return 0;
}


你可能感兴趣的:(数学,HDU)