HDU 5104 Primes Problem(拆分素数和问题)【BestCoder Round #18 1001】
2016-12-20 10:54
525 查看
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5104
【中文题意】给你一个整数n(n<=10000),让你求p1+p2+p3=n的组合方式有多少种,其中p1,p2,p3都是素数,且满足p1<=p2<=p3。
【思路分析】先把一万以内的素数筛出来,然后暴力枚举其中两个素数,看第三个数是不是素数就好了。
【AC代码】
【中文题意】给你一个整数n(n<=10000),让你求p1+p2+p3=n的组合方式有多少种,其中p1,p2,p3都是素数,且满足p1<=p2<=p3。
【思路分析】先把一万以内的素数筛出来,然后暴力枚举其中两个素数,看第三个数是不是素数就好了。
【AC代码】
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; bool isprime[10005]; int prime[10005],nprime=0; void doprime() { memset(isprime,true,sizeof(isprime)); isprime[1]=false; for(int i=2; i<=10000; i++) { if(isprime[i]) { prime[nprime++]=i; for(int j=2*i; j<=10000; j+=i) { isprime[j]=false; } } } } int main() { doprime(); int n; while(~scanf("%d",&n)) { int re=0; for(int i=0;i<nprime;i++) { if(prime[i]>=n)break; for(int j=i;j<nprime;j++) { if(prime[i]+prime[j]>=n||prime[j]>=n)break; if(isprime[n-prime[i]-prime[j]]==true&&(n-prime[i]-prime[j])>=prime[j]) { //printf("%d %d %d\n",prime[i],prime[j],n-prime[i]-prime[j]); re++; } } } printf("%d\n",re); } return 0; }
相关文章推荐
- HDU 5104 Bestcoder #18 1001 Primes Problem 水题
- hdu 5104 Primes Problem (素数+递推)
- hdu 5104 Primes Problem(素数判定)
- HDU5105 Math Problem && BestCoder Round #18 1002
- BestCoder Round #56 1002 hdu 5464 (背包问题)
- Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)
- BestCoder Round #18 1004 &&hdu 5107 K-short Problem 线段树
- [Bestcoder] Valentine's Day Round 1001 - Ferries Wheel | HDU 5174
- hdu 5104 Primes Problem (素数 打表 水)
- BestCoder Round#29 1001 GTY's math problem
- BestCoder Round #78 1001/ hdu 5655 CA Loves Stick
- HDU 5104 Primes Problem(素数打表)
- HDU 5105 Math Problem(BestCoder Round #18)
- BestCoder Round #18 1001 primes problem
- hdu 4883 bestcoder round 2 1001 TIANKENG’s restaurant
- [BestCoder] Round #25 1001 - Harry and Magical Computer | HDU 5154
- BestCoder #Valentine's Day Round 1001 || hdu 5174
- hdu 4956 Poor Hanamichi(bestcoder Round 6 1001)
- BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611
- HDU5104 Primes Problem && BestCoder Round #18 1001