USACO Section 1.5 - Superprime Rib(DFS + 枚举)

题意

找出n位,每次去掉最后一位都是素数的数字。

思路

首先有几个特殊的数字。

  1. 1 9不能作为第一位。

  2. 2只能作为第一位。

其余的数字都不行。

然后DFS即可。

一开始我是先枚举出数字再判断,T了。应该边枚举边判断

代码

 
 
  1. /*
  2. ID: mycodeb1
  3. LANG: C++
  4. TASK: sprime
  5. */
  6. #include <cstdio>
  7. #include <stack>
  8. #include <set>
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <queue>
  13. #include <functional>
  14. #include <cstring>
  15. #include <algorithm>
  16. #include <cctype>
  17. #include <ctime>
  18. #include <cstdlib>
  19. #include <fstream>
  20. #include <string>
  21. #include <sstream>
  22. #include <map>
  23. #include <cmath>
  24. #define LL long long
  25. #define lowbit(x) ((x) & (-x))
  26. #define MP(a, b) make_pair(a, b)
  27. #define MS(arr, num) memset(arr, num, sizeof(arr))
  28. #define PB push_back
  29. #define F first
  30. #define S second
  31. #define ROP freopen("input.txt", "r", stdin);
  32. const double PI = acos(-1.0);
  33. const int INF = 0x3f3f3f3f;
  34. using namespace std;
  35. const int MAXN = 1000 + 5;
  36. typedef pair<int, int> pii;
  37. typedef vector<int>::iterator viti;
  38. typedef vector<pii>::iterator vitii;
  39. const int num[] = {1, 2, 3, 5, 7, 9};
  40. int str[10], n;
  41. bool Check(int nm)
  42. {
  43. for (int i = 2; i <= (int)sqrt(nm + 0.5); i++)
  44. if (nm % i == 0) return false;
  45. return true;
  46. }
  47. void DFS(int curLen)
  48. {
  49. int i, j;
  50. if (curLen == n)
  51. {
  52. int nm = 0;
  53. for (i = 0; i < n; i++) nm = nm * 10 + str[i];
  54. if (Check(nm)) printf("%d\n", nm);
  55. return;
  56. }
  57. for (i = 0; i < 6; i++)
  58. {
  59. if (curLen == 0)
  60. if (i == 0 || i == 5) continue;
  61. if (curLen != 0 && i == 1) continue;
  62. str[curLen] = num[i];
  63. int nm = 0;
  64. for (j = 0; j <= curLen; j++) nm = nm * 10 + str[j];
  65. if (!Check(nm)) continue;
  66. DFS(curLen + 1);
  67. }
  68. }
  69. int main()
  70. {
  71. //ROP;
  72. freopen("sprime.out", "w", stdout);
  73. freopen("sprime.in", "r", stdin);
  74. MS(str, 0);
  75. int i, j;
  76. scanf("%d", &n);
  77. DFS(0);
  78. return 0;
  79. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值