首页 > 其他 > 详细

HDU 5104 Primes Problem(数学)

时间:2015-01-25 11:09:47      阅读:235      评论:0      收藏:0      [点我收藏+]

题目链接: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

代码如下:

#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 5104 Primes Problem(数学)

原文:http://blog.csdn.net/u012860063/article/details/43112469

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!