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
 
Accepted 的代码:
 
#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;
int flag=0;
for(i=0; i<=n; i++)
{
if(s[i]>=n)
break;
for(j=i; j<=n; j++)
{
if( (s[i]+s[j])>=n )
{
flag=1;
break;
}
else
{
int dd=n-s[i]-s[j];
if(f[dd]==0 && dd>=s[i] && dd>=s[j] )
{
cnt++;
} }
}
}
cout<<cnt<<endl;
}
return 0;
}
 
这个代码是超时的(3层循环太 耗时):
#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;
}

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

  1. BestCoder Round #1 第一题 逃生

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

  2. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  3. BestCoder Round #1 第二题 项目管理

    // 第二题 我记得很久很久很久以前看过这样的题目,忘记是哪的区域赛了 // 记得有人说和节点度数有关,我记不清了,反正当时完全不懂 // 然后我想了想,估计就是更新节点度数有关,YY出来可能只要更新 ...

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

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

  5. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(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 ...

  6. HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数

    GTW likes function      Memory Limit: 131072/131072 K (Java/Others) 问题描述 现在给出下列两个定义: f(x)=f_{0}(x)=\ ...

  7. Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律

    题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...

  8. Help Hanzo (素数筛+区间枚举)

    Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...

  9. UVALive-3399-Sum of Consecutive Prime Numbers(素数筛,暴力)

    原题链接 写个素数筛暴力打表一波就AC了: #include <iostream> using namespace std; const int N = 10001; int i, j, ...

随机推荐

  1. HDU 1045 Fire Net 状压暴力

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  2. 转:PHP 生成复杂JSON格式 简单快速方法

    PHP 生成JSON 格式主要使用json_encode()函数.这个函数的输入参数支持PHP数组和对象类型. 查阅网上的例子通常都是使用数组的,也有个别使用对象生成.但实际项目中,我们要生成的JSO ...

  3. Java中使用new Date()和System.currentTimeMillis()获取当前时间戳的区别(转)(Java进阶-性能提升)

    在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的 ...

  4. 深入理解Activity启动流程(三)–Activity启动的详细流程2

    本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--A ...

  5. ubuntu安装 uwsgi

    http://www.jianshu.com/p/e6ff4a28ab5a/ sudo apt-get install python-dev #不安装这个,下面的安装可能会失败 sudo pip in ...

  6. 【转载】GitHub中国区前100名到底是什么样的人

    转载了这篇文章: http://www.jianshu.com/p/d29cba7934c9 这篇文章真是太牛了!转载过来涨涨见识,同时好好励志一把.还有,ruanyifeng怎么长那样... 哈 另 ...

  7. libsvm交叉验证与网格搜索(参数选择)

    首先说交叉验证.交叉验证(Cross validation)是一种评估统计分析.机器学习算法对独立于训练数据的数据集的泛化能力(generalize), 能够避免过拟合问题.交叉验证一般要尽量满足:1 ...

  8. 百科知识 ass文件如何打开

    直接拖入视频即可播放 鼠标右键 用记事本打开 也有一些软件支持比如POPSUB(也比较方便调整时间轴) 如果你是说如何加载字幕的话 用VOBSUB是最好的... ASS是视频的字幕,和视频放在同一文件 ...

  9. table 设置边框

    本文引自:https://www.cnblogs.com/leona-d/p/6125896.html 示例代码: <!DOCTYPE html> <html lang=" ...

  10. C++11 并发指南系列(转)

    本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...