Advertisement
Alex_tz307

Deleatable Primes

Oct 24th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. bool prim(int a) {
  7.     if(a == 0 || a == 1)
  8.         return false;
  9.     if(a == 2 || a == 3)
  10.         return true;
  11.     if(a % 2 == 0 || a % 3 == 0)
  12.         return false;
  13.     int rad = sqrt(a);
  14.     for(int d = 5; d <= rad; d += 6)
  15.         if(a % d == 0 || a % (d + 2) == 0)
  16.             return false;
  17.     return true;
  18. }
  19.  
  20. int ans;
  21.  
  22. inline void solve(string& s) {
  23.     if(s.size() == 1) {
  24.         if(strchr("2357", s[0]))
  25.             ++ans;
  26.         return;
  27.     }
  28.     int N = s.size();
  29.     for(int i = 0; i < N; ++i) {
  30.         int val = 0;
  31.         for(int j = 0; j < N; ++j)
  32.             if(j != i)
  33.                 val = val * 10 + (s[j] - '0');
  34.         if(prim(val)) {
  35.             string new_s;
  36.             for(int j = 0; j < N; ++j)
  37.                 if(j != i)
  38.                     new_s.push_back(s[j]);
  39.             solve(new_s);
  40.         }
  41.     }
  42. }
  43.  
  44. int32_t main() {
  45.     string s;
  46.     cin >> s;
  47.     solve(s);
  48.     cout << ans;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement