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. Network | sk_buff

    sk_buff结构可能是linux网络代码中最重要的数据结构,它表示接收或发送数据包的包头信息.它在中定义,并包含很多成员变量供网络代码中的各子系统使用.    这个结构被不同的网络层(MAC或者其他 ...

  2. CEF General Usage(CEF3预览)

    CEF General Usage(CEF3预览) 介绍 CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chro ...

  3. Linux下挂载指定分区下的某个文件夹到指定目录(mount)

    # 挂载 mount --bind olddir newdir # 卸载 umount newdir 参考: http://www.cnblogs.com/dabaopku/archive/2010/ ...

  4. xamarin.IOS App闪退日志----crash

    一.查找日志文件位置:通过xcode/windows/device/你的IPhone/crash 二.拿到日志可以直接查看,但是日志记录太乱看不懂,需要转换处理,查找.DSYM文件,文件位置:/Use ...

  5. 更新tensorflow支持GPU时出错

    sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...

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

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

  7. 算法之美--2.3.1 Z字形编排问题

    2016-12-08   00:23:11 写在前面的话:万事贵在坚持,万事开头难,有很多的东西要学,要知道主次,讲究效率,大的方向对就行!坚持........ 一.图像压缩编码中的Z字排序 JPEG ...

  8. Git以及github的使用方法(五),暂存区和工作区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...

  9. sass的使用(载)

    一.什么是SASSSASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护.本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一般使 ...

  10. Unity光滑与粗糙的材质——相似于生锈的金属表面

    纹理是在Photoshop中制作的,终于效果则是在Unity里得到的.这样的类型的材质.在3D游戏中非经常见.