Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)

Primes Problem

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

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(n≤10000).

Output

For each test case, print the number of ways.

Sample Input

3
9

Sample Output

0
2

代码:

#include <string>
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int f[10001];

void sushu()
{
    int i, j;
    memset(f, 0, sizeof(f));
    f[1]=1;
    i=2;
    while(i<=200)
    {
        for(j=i*2; j<=10000; j+=i)
        {
            f[j]=1;
        }
        i++;
        while(f[i]==1)
        {
            i++;
        }
    }
}

int s[10000], e;

int main()
{
    int n;
    int i, j, k;
    int cnt;
    sushu();
     e=0;
    for(i=2; i<=10000; i++)
    {
        if(f[i]==0)
        {
            s[e++]=i;
        }
    }
    while(scanf("%d", &n)!=EOF)
    {
        if(n<6)
        {
            cout<<‘0‘<<endl;
            continue;
        }
        cnt=0;
        for(i=0; i<=n; i++)
        {

            for(j=i; j<=n; j++)
            {
                for(k=j; k<=n; k++)
                {
                    if((s[i]+s[j]+s[k])==n)
                    {
                        cnt++;
                    }
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}
时间: 2024-10-14 08:26:53

Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)的相关文章

HDU-5104-Primes Problem (BestCoder Round #18!!)

Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 817    Accepted Submission(s): 382 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1

hdu4931 Happy Three Friends(BestCoder Round#4签到题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4931 Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 150    Accepted Submission(s): 128 Problem Description Dong-hao , Grandpa

BestCoder Round #1 第一题 逃生

// 等了好久,BESTCODER 终于出来了..像咋这样的毕业的人..就是去凑凑热闹// 弱校搞acm真是难,不过还是怪自己不够努力// 第一题是明显的拓扑排序,加了了个字典序限制而已// 用优先队列就可以搞定了#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <vector> #include <map>

BestCoder Round #1 第二题 项目管理

// 第二题 我记得很久很久很久以前看过这样的题目,忘记是哪的区域赛了 // 记得有人说和节点度数有关,我记不清了,反正当时完全不懂 // 然后我想了想,估计就是更新节点度数有关,YY出来可能只要更新相邻节点度数更大或更小的就可以了 // 复杂度不知道多少,就是提交了试试,15MS就过了 // 看来就是这样了 // 具体就是求出每个节点度数,然后每次更新时,只要更新与自己相连,但度数比自己大的 // 查询时把度数自己大的节点累加的值加上就可输出,具体看代码比较清楚 // 也就做得来这2题了(见识

BestCoder Round #31 B题

beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 136    Accepted Submission(s): 78 Problem Description Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is the number of A's digits). We call A as "

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj) (i,j∈[1,n]) We define that lowbit(x)=2^k,k is the smallest integer satisfied ((x and 2^k)>0)Specially,

BestCoder Round #18(hdu5105)Math Problem(高中数学)

最大值无非就是在两个端点或极值点处取得. 我注意讨论了a=0和b=0,却忽略了极值点可能不在L到R的范围内这一问题.被Hack了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include

BestCoder Round #14 B 题 Harry And Dig Machine 【TSP】

题目:Harry And Dig Machine 哈哈  终于涨边粉色了,不容易呀.顺便写一道题解吧 题意:给一个m*n的矩阵,然后其中最多由10个有值,求总左上角把所有的值都拿上回到左上角的最小步数. 标准的TSP回到原点问题,需要先预处理出图来,然后TSP即可. AC代码: #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <io

BestCoder Round #18

1001 Primes Problem 打个10^4的素数表,枚举前两个搞一下就好了. #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<vector> #include<stdlib.h> #include<algorithm> #include<math.h> using namespace std; c