hdu 5104(数学)

Primes Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2841    Accepted Submission(s): 1276


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
 
题意:找到三个素数 i,j,k 满足 i<=j<=k 并且 i+j+k == n 输入n,问满足这个条件的有多少。
题解:开始的时候直接枚举 i (1-n/2) 炸掉了,后来估计了一下 i j 不会超过 n/2。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
bool p[10005];
void init(){
    p[1] = true;
    for(int i=2;i<=10000;i++){
        if(!p[i]){
            for(int j=i*i;j<=10000;j+=i) p[j] = true;
        }
    }
}
int main(){
    init();
    int n;
    while(scanf("%d",&n)!=EOF){
        int cnt = 0;
        for(int i=2;i<n/2;i++){
            for(int j=i;j<n/2;j++){
                int k = n-i-j;
                if(!p[i]&&!p[j]&&!p[k]&&k>=j){
                    cnt++;
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

 

posted @ 2016-07-11 17:16  樱花庄的龙之介大人  阅读(148)  评论(0编辑  收藏  举报