HDU 5104 - Primes Problem (枚举)

来源:互联网 发布:淘宝宝贝链接怎么缩短 编辑:程序博客网 时间:2024/05/29 09:08

题意

找出n1 + n2 + n3 = n && n1 and n2 and n3为素数的三元组数

思路

先打表出1W以内的素数,然后枚举n2、n3。判断n - n2 - n3是否为素数,如果是的话判断该数是否 <= n2。是的话ans + 1

一开始用lower_bound竟然T了?

代码

  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define ULL unsigned long long
  17. #define SZ(x) (int)x.size()
  18. #define Lowbit(x) ((x) & (-x))
  19. #define MP(a, b) make_pair(a, b)
  20. #define MS(arr, num) memset(arr, num, sizeof(arr))
  21. #define PB push_back
  22. #define F first
  23. #define S second
  24. #define ROP freopen("input.txt", "r", stdin);
  25. #define MID(a, b) (a + ((b - a) >> 1))
  26. #define LC rt << 1, l, mid
  27. #define RC rt << 1|1, mid + 1, r
  28. #define LRT rt << 1
  29. #define RRT rt << 1|1
  30. #define BitCount(x) __builtin_popcount(x)
  31. #define BitCountll(x) __builtin_popcountll(x)
  32. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  33. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  34. const double PI = acos(-1.0);
  35. const int INF = 0x3f3f3f3f;
  36. using namespace std;
  37. const double eps = 1e-8;
  38. const int MAXN = 10000 + 10;
  39. const int MOD = 1000007;
  40. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; //0123£¬ÉÏÏÂ×óÓÒ
  41. typedef pair<int, int> pii;
  42. typedef vector<int>::iterator viti;
  43. typedef vector<pii>::iterator vitii;
  44. vector<int> pri;
  45. int n, vis[MAXN];
  46. void GetPrime()
  47. {
  48. vis[1] = 1;
  49. for (int i = 2; i <= MAXN; i++)
  50. if (!vis[i])
  51. {
  52. pri.PB(i);
  53. for (int j = i * 2; j <= MAXN; j += i) vis[j] = 1;
  54. }
  55. }
  56. int main()
  57. {
  58. //ROP;
  59. ios::sync_with_stdio(0);
  60. int i, j;
  61. GetPrime();
  62. while (cin >> n)
  63. {
  64. int ans = 0;
  65. for (int i = 0; pri[i] < n && i < SZ(pri); i++)
  66. for (j = i; pri[j] + pri[i] < n && j < SZ(pri); j++)
  67. {
  68. if (vis[n - pri[i] - pri[j]] == 0 && n - pri[i] - pri[j] <= pri[i])
  69. ans++;
  70. }
  71. cout << ans << endl;
  72. }
  73. return 0;
  74. }
0 0
原创粉丝点击