HDU5104 Primes Problem && BestCoder Round #18 1001
2014-11-16 16:28
459 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104
解题思路:BestCoder官方题解:
要用素数筛选法,不然会超时,然后再按题解求出即可。
素数筛选法:http://blog.csdn.net/tongyongzh/article/details/6693409
AC代码:
解题思路:BestCoder官方题解:
首先将10000内的素数筛出来(约1000个),(p1,p2,p3)枚举三元组前两个p1,p2,可知若存在p3满足条件,必有p3=n−p1−p2,故令t=n−p1−p2。若t为不小于p2的素数,则t满足p3的条件。则答案加一。
要用素数筛选法,不然会超时,然后再按题解求出即可。
素数筛选法:http://blog.csdn.net/tongyongzh/article/details/6693409
AC代码:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; const int maxn=10010; int pr[maxn]={0}; int prime[1500]; int main() { int i,j,t; t=sqrt(maxn*1.0)+1; for(i=2;i<=t;i++) for(j=2;j<=maxn/i;j++) pr[j*i]=1; j=0; for(i=2;i<=maxn;i++) if(pr[i]==0) prime[j++]=i; //cout<<j<<endl; int n; while(scanf("%d",&n)!=EOF) { int p,q,ans=0; double m=n/3; for(i=0;i<=j;i++) { if(prime[i]>m) break; for(p=i;p<=j;p++) { if(prime[p]>=n) break; q=n-prime[i]-prime[p]; if(q<prime[p]) continue; if(pr[q]==0) ans++;//cout<<prime[i]<<endl;cout<<prime[p]<<endl;} } } printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU 5104 Primes Problem(拆分素数和问题)【BestCoder Round #18 1001】
- BestCoder Round #18 1001 primes problem
- BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611
- BestCoder Round #18 1004 &&hdu 5107 K-short Problem 线段树
- BestCoder Round#29 1001 GTY's math problem
- HDU5105 Math Problem && BestCoder Round #18 1002
- BestCoder Round #18
- Bestcoder Round 47 && 48
- hdu 4956 Poor Hanamichi(bestcoder Round 6 1001)
- BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)
- hdu 5196 DZY Loves Inversions && BestCoder Round #35
- Bestcoder Round 15# 1001 Love
- BestCoder Round #34 1001 Go to movies
- hdu 4883 bestcoder round 2 1001 TIANKENG’s restaurant
- Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)
- BestCoder Round #50 (div.2) 1001
- Bestcoder Round #48 1001、1002
- [Bestcoder] Valentine's Day Round 1001 - Ferries Wheel | HDU 5174
- HDU 5104 Bestcoder #18 1001 Primes Problem 水题
- BestCoder #Valentine's Day Round 1001 || hdu 5174