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. Vue开发之路由进阶

    1.路由组件传参 在一个页面中,需要根据路由获得参数,然后在页面进行逻辑处理,可以通过$route来获取相关参数 但是这样一来,页面组件与路由耦合太高,为了解耦,页面组件可以在更大程度上进行复用,可以 ...

  2. JVM, JRE 和JDK

    JVM -- java virtual machine A Java virtual machine (JVM) is a process virtual machine that can execu ...

  3. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  4. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest

    A Hanoi Tower 递归 题意: 大家都很熟悉汉诺塔的递归程序,现在给你一个组合,询问你这个组合是否会出现在汉诺塔的递归过程中. 题解: 将汉诺塔的递归程序反过来思考,考虑当前最大的那个盘,我 ...

  5. chrome mac 快捷键

    ⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Google Chrome 浏览器中打开计算机中的文件. 按住 ⌘ 键,然后 ...

  6. javascript 函数初探 (三)--- javascript 变量的作用域

    javascript 变量的作用域: 这是一个至关重要的问题.特别是当我们从别的语言转向javascript时,必须要明白一点,即在javascript中,变量的定义并不是以代码块作为作用域的,而是以 ...

  7. SpringUtils写法

    @Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...

  8. CentOS 笔记

    对安装CentOS安装使用过程中的问题做一个笔记,第一次安装,安装的是7.0版本,最小化安装. 安装环境 :Windows 2012 R2 Standard,Hyper-V Virstual Mach ...

  9. Silverlight 离线安装包

    直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...

  10. android clipRect Op.xxx各个参数理解

    有点小啰嗦的一篇学习笔记,可以直接看最后得出的结论:前面的各种图片和说明都是为最后的结论服务的 1)剪切:和平常画图工具剪切的作用一样,在画布上剪切一个区域,比如剪切一个Rect区域,画布canvas ...