Primes Problem (HDU5104) 素数问题
来源:互联网 发布:张家口 承德 知乎 编辑:程序博客网 时间:2024/06/07 00:25
Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 796 Accepted Submission(s): 368
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
39
Sample Output
02
找到小于10000的素数,用素数筛选法。
避开三重循环,采用二重循环,如果存在p3满足条件,则必定会有p3 = n - p1 - p2,如果p3是素数并且p3大于等于p2,则计数器加一。
因为使用三重循环直接查找,到时TLE了一回。。。。。。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int su[10000] = {2,3,5};int prim[10000];int init(){ memset(prim,0,sizeof(prim)); prim[2] = 1; prim[3] = 1; prim[5] = 1; int i,j,k,m = 2,bj; k = 3; for(i = 7; i <= 10000; i = i + m) { bj = 1; m = 6 - m; for(j = 0; su[j]*su[j] <= i; j++) { if(i%su[j] == 0) { bj = 0; break; } } if(bj == 1) { su[k++] = i; prim[i] = 1; } } return k;}int main(){ int t,i,j,m; int k = init(); while(~scanf("%d",&t)) { int num = 0; for(i = 0; i < k; i++) { for(j = i; j < k; j++) { m = t - su[i] - su[j]; if(m < 0) { break; } if(prim[m] == 1 && m >= su[j]) num++; } } printf("%d\n",num); } return 0;}
0 0
- Primes Problem (HDU5104) 素数问题
- HDU5104 Primes Problem(素数筛选法)
- hdu5104——Primes Problem(素数)
- HDU5104-Primes Problem
- HDU5104-Primes Problem
- hdu 5104 Primes Problem(素数判定)
- HDU 5104 Primes Problem(拆分素数和问题)【BestCoder Round #18 1001】
- hdoj Primes Problem 5104 (素数打表&&技巧)
- HDU 5104 Primes Problem(素数打表)
- hdoj-5104Primes Problem(素数打表)
- Primes(素数)
- hdoj--5104--Primes Problem(素数打表)
- Problem 10 Summation of primes (素数和)
- UVa Problem 10168 Summation of Four Primes (四素数之和)
- Primes (素数筛法)
- [LeetCode] Count Primes - 素数系列问题
- (Problem 35)Circular primes
- (Problem 37)Truncatable primes
- Memcache and Mongodb
- UML——宏观总结
- UISegmentedControl 基本属性,
- 数据结构之单链表
- Python学习笔记-第五章 条件、循环和其他语句(下)
- Primes Problem (HDU5104) 素数问题
- 第13周项目1-数组大折腾-(3)
- 我是项目新手,我的项目经验总结
- P2P系统中的典型的DHT系统-----------Chord算法
- 应用层
- Android:Layout_weight的深刻理解
- 循环与判断3
- 软件工程:(六)面向对象技术
- 软件工程——————软件生长的灯塔