hdu 5104 Primes Problem

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5104 

Primes Problem

Description

Given a number n, please count how many tuple$(p_1, p_2, p_3)$ satisfied that $p_1 \leq p_2  \leq p_3, $ $p_1,p_2,p_3$ are primes and $p_1 + p_2 + p_3 = n$.

Input

Multiple test cases(less than 100), for each test case, the only line indicates the positive integer $n\ (n \leq 10000).$

Output

For each test case, print the number of ways.

Sample Input

3
9

Sample Output

0
2

简单题如下。。

 1 #include<algorithm>

 2 #include<iostream>

 3 #include<cstdlib>

 4 #include<cstring>

 5 #include<cstdio>

 6 #include<vector>

 7 #include<map>

 8 #include<set>

 9 using std::cin;

10 using std::cout;

11 using std::endl;

12 using std::find;

13 using std::sort;

14 using std::set;

15 using std::map;

16 using std::pair;

17 using std::vector;

18 using std::multiset;

19 using std::multimap;

20 #define pb(e) push_back(e)

21 #define sz(c) (int)(c).size()

22 #define mp(a, b) make_pair(a, b)

23 #define all(c) (c).begin(), (c).end()

24 #define iter(c) decltype((c).begin())

25 #define cls(arr,val) memset(arr,val,sizeof(arr))

26 #define cpresent(c, e) (find(all(c), (e)) != (c).end())

27 #define rep(i, n) for (int i = 0; i < (int)(n); i++)

28 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)

29 const int N = 10010;

30 typedef unsigned long long ull;

31 bool P[N];

32 int tot, prime[1500];

33 bool isPrim(int n) {

34     for (int i = 2; i * i <= n; i++) if (n % i == 0) return false;

35     return n != 1;

36 }

37 inline void init() {

38     for (int i = 1; i < N;i++) {

39         if (isPrim(i)) prime[tot++] = i, P[i] = true;

40         else P[i] = false;

41     }

42 }

43 inline void solve(int n) {

44     int res = 0;

45     for (int i = 0; i < tot && prime[i] < n; i++) {

46         for (int j = i; j < tot && prime[j] < n; j++) {

47             int t = n - prime[i] - prime[j];

48             if (t >= prime[j] && P[t]) res++;

49         }

50     }

51     printf("%d\n", res);

52 }

53 int main() {

54 #ifdef LOCAL

55     freopen("in.txt", "r", stdin);

56     freopen("out.txt", "w+", stdout);

57 #endif

58     int n;

59     init();

60     while (~scanf("%d", &n)) {

61         solve(n);

62     }

63     return 0;

64 }
View Code

你可能感兴趣的:(Prim)