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. Java-线程池总结

    线程池的优点: 重用线程,减少线程创建和销毁的性能开销. 管理线程,并提供定时执行以及指定间隔循环执行等功能. Android中的线程来源于Java中的Executor,实现类是ThreadPoolE ...

  2. this.class.getClassLoader().getResourceAsStream

    this.getClass().getClassLoader().getResource("template");    首先,调用对象的getClass()方法是获得对象当前的类 ...

  3. TdxBarButton的FASTSCRIPT封装

    TdxBarButton的FASTSCRIPT封装 // cxg 2017-2-13 unit fs_dev; interface{$i fs.inc}uses fs_iinterpreter, fs ...

  4. Android 打开其他程序

    Intent intent = new Intent(); intent.setComponent(new ComponentName("所要打开的程序包名", "所要打 ...

  5. Intent 传递对象

    方法: 可以让这个要传递的对象所属类实现Serializable或者Parcelable接口, 然后利用onCreate函数中的Bundle参数作为载体,传递这个对象. 例如: <span st ...

  6. module has no attribute 'seq2seq'

    tensorflow 中tf.nn.seq2seq.sequence_loss_by_example to tf.contrib.legacy_seq2seq.sequence_loss_by_exa ...

  7. 阿里云云服务器ubuntu配置nginx+uwsgi+django记录文档

    1 安装ssh 1  sudo apt-get update 2  sudo apt-get install openssh-server 3  sudo ps -e |grep ssh  有sshd ...

  8. paramiko获取远程主机的环境变量

    本文的情况,不同的linux系统版本,表现可能不同. 问题:默认情况下,paramiko在远程主机上执行命令的时候,命令的搜索路径为(/usr/local/bin:/bin:/usr/bin),这样我 ...

  9. LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL

    运算符转换 1.AsEnumerable:将类型转换为泛型 IEnumerable 使用 AsEnumerable<TSource> 可返回类型化为泛型 IEnumerable 的參数.在 ...

  10. 如何让你的服务屏蔽Shodan扫描

    1. 前言 在互联网中,充斥着各种各样的网络设备,shodan等搜索引擎提供给了我们一个接口,让我们可以在输入一些过滤条件就可以检索出网络中相关的设备. 对于我们的一些可能有脆弱性或者比较隐私的服务, ...