Advertisement
Mirbek

ИНТЕРЕСНЫЕ ЧИСЛА

Feb 8th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool isPrime(int x) {
  6.     for (int i = 2; i < x; i++) {
  7.         if (x % i == 0) {
  8.             return false;
  9.         }
  10.     }
  11.     return true;
  12. }
  13.  
  14. bool isInteresting(string s) {
  15.     int cnt = count(s.begin(), s.end(), '0');
  16.     if (cnt > 0) return false;
  17.     for (int i = 0; i < s.size(); i++) {
  18.         for (int j = i + 1; j < s.size(); j++) {
  19.             int a = s[i] - '0';
  20.             int b = s[j] - '0';
  21.             if (__gcd(a, b) != 1 || !isPrime(a + b)) {
  22.                 return false;
  23.             }
  24.         }
  25.     }
  26.     return true;
  27. }
  28.  
  29. int main(){
  30.     int n;
  31.     cin >> n;
  32.  
  33.     vector <string> vec;
  34.     for (int i = 11; i <= 99; i++) {
  35.         string x = to_string(i);
  36.         if (isInteresting(x)) {
  37.             vec.push_back(x);
  38.         }
  39.     }
  40.  
  41.     for (int i = 0; vec.size() <= n; i++) {
  42.         string s = vec[i];
  43.         for (char d = '1'; d <= '9'; d++) {
  44.             if (isInteresting(s + d)) {
  45.                 vec.push_back(s + d);
  46.             }
  47.         }
  48.     }
  49.  
  50.     cout << vec[n - 1] << endl;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement