fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define all(x) x.begin(), x.end()
  4. #define f1(i, n) for(int i=1;i<=n;++i)
  5. using namespace std;
  6.  
  7. const int maxn = 2e6 + 1;
  8.  
  9. bool checkPrime(ll n) {
  10. if (n <= 1) return false;
  11. for (int i = 2; i <= sqrt(n); ++i) {
  12. if (n % i == 0) return false;
  13. }
  14. return true;
  15. }
  16.  
  17. int cnt_digit(ll n) {
  18. int cnt = 0;
  19. while (n != 0) {
  20. n /= 10;
  21. cnt++;
  22. }
  23. return cnt;
  24. }
  25.  
  26. ll digit[100], bin[1000], res = 0, limit;
  27.  
  28. void ql(int i) {
  29. if (i > limit) {
  30. ll num = 0;
  31. for (int i = 1; i <= limit; ++i) {
  32. if (bin[i] == 1) {
  33. num = num * 10 + digit[i];
  34. }
  35. }
  36. if (checkPrime(num)) {
  37. res = max(res, num);
  38. }
  39. return;
  40. }
  41.  
  42. bin[i] = 1;
  43. ql(i + 1);
  44.  
  45. bin[i] = 0;
  46. ql(i + 1);
  47. }
  48.  
  49.  
  50. int main() {
  51. ios::sync_with_stdio(false);
  52. cin.tie(nullptr);
  53.  
  54. ll n;
  55. cin >> n;
  56. int k = cnt_digit(n);
  57. int i = k;
  58. while (n != 0) {
  59. digit[i] = n % 10;
  60. n /= 10;
  61. --i;
  62. }
  63. limit = k;
  64. ql(1);
  65. cout << res;
  66. }
Success #stdin #stdout 0.24s 5316KB
stdin
Standard input is empty
stdout
1473468933289