zoukankan      html  css  js  c++  java
  • 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
    ",cnt);
        }
        return 0;
    }
  • 相关阅读:
    poj 1573 Robot Motion
    poj 1035 Spell checker
    poj 3080 Blue Jeans
    poj 3468 A Simple Problem with Integers(线段树区间更新)
    poj 3687 Labeling Balls(拓补排序)
    poj 3295 Tautology
    poj 1062 昂贵的聘礼
    hdu 1170 Balloon Comes!
    hdu 1215 七夕节
    OCJP-试题集合 | 对象的比较
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5660945.html
Copyright © 2011-2022 走看看