hdu_5104 Primes Problem()

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104

rimes Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2844    Accepted Submission(s): 1277

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

题意:输入一个数字n,找出三个数字p1,p2,p3,满足p1<=p2<=p3并且p1+p2+p3=n

 1 //筛素数
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N = 10005;
 7 bool pri[N];
 8 int prime[N];
 9 int cnt;
10 void init()
11 {
12     cnt = 0;
13     pri[0] = pri[1] = 1;
14     for(int i = 2; i < N; i++)
15     {
16         if(!pri[i]){
17             prime[cnt++] = i;
18             for(int j = i+i; j < N; j+=i)
19             {
20                 pri[j] = 1;
21             }
22         }
23     }
24     return;
25 }
26 int main()
27 {
28     int n;
29     init();
30     while(~scanf("%d",&n))
31     {
32         int ans = 0;
33
34         //printf("%d\n",cnt);
35         for(int i = 0; i < cnt; i++)
36         {
37             if(3*prime[i]>n) break;
38             for(int j = i; j < cnt; j++)
39             {
40                 if(prime[i]+2*prime[j]>n) break;
41                 //for(int k = j; k < cnt; k++)
42                 //{
43                 //    if(prime[i]+prime[j]+prime[k]==n) ans++;// printf("%d %d %d\n",prime[i],prime[j],prime[k]);printf("%d %d %d\n",i,j,k);}
44                // }
45                if(!pri[n-prime[i]-prime[j]]) ans++;
46             }
47         }
48         printf("%d\n",ans);
49     }
50     return 0;
51 }
时间: 2024-10-11 15:24:43

hdu_5104 Primes Problem()的相关文章

HDU 5104 Primes Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104 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

hdu 5104 Primes Problem (素数+递推)

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 int

hdu 5104 Primes Problem(prime 将三重循环化两重)

//宁用大量的二维不用量小的三维 #include <iostream> #include<cstdio> #include<cstring> using namespace std; int prime[1000],num[10005]; int Cout; void initPrime() { for(int i=2;i<=10000;i++) num[i]=1; for(int i=2;i<=10000;i++) { if(num[i]) { for(

hdu 5104 Primes Problem (素数 打表 水)

http://acm.hdu.edu.cn/showproblem.php?pid=5104 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int isp[10000+100]; int prime[10000]; int coun; bool isprime(int x)

欧拉计划(python) problem 27

Quadratic primes Problem 27 Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible

HDU-5104-Primes Problem (BestCoder Round #18!!)

Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 817    Accepted Submission(s): 382 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1

Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)

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<=p

BestCoder Round #18

1001 Primes Problem 打个10^4的素数表,枚举前两个搞一下就好了. #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<vector> #include<stdlib.h> #include<algorithm> #include<math.h> using namespace std; c

hdu 5104(数学)

Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2841    Accepted Submission(s): 1276 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p