码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5104 Primes Problem(数学)

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

标签:hdu   数学   

题目链接: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(数学)

标签:hdu   数学   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!