senb1

krsu 635

Mar 10th, 2023
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. /*
  2. by: senb1
  3. */
  4.  
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <string>
  9. #include <numeric>
  10.  
  11. #define ll long long
  12. #define yes cout<<"Yes\n"
  13. #define no cout<<"No\n"
  14. #define all(x) x.begin(), x.end()
  15.  
  16. using namespace std;
  17.  
  18. ll bp(ll a, ll n) {
  19.     if (n == 0) return 1;
  20.     if (n % 2) return bp(a, n - 1) * a;
  21.     ll b = bp(a, n / 2);
  22.     return b * b;
  23. }
  24.  
  25. ll gcd(ll a, ll b) {
  26.     return b ? gcd(b, a % b) : a;
  27. }
  28. ll lcm(ll a, ll b) {
  29.     return a / gcd(a, b) * b;
  30. }
  31.  
  32. bool isPrime(int x) {
  33.     for (int i = 2; i * i <= x; i++) {
  34.         if (x % i == 0) return false;
  35.     }
  36.     return true;
  37. }
  38.  
  39. bool isInteresting(string s) {
  40.     int cnt = count(s.begin(), s.end(), '0');
  41.     if (cnt > 0) return false;
  42.     for (int i = 0; i < s.size(); i++) {
  43.         for (int j = i + 1; j < s.size(); j++) {
  44.             int a = s[i] - '0';
  45.             int b = s[j] - '0';
  46.             if (gcd(a, b) != 1 || !isPrime(a + b)) {
  47.                 return false;
  48.             }
  49.         }
  50.     }
  51.     return true;
  52. }
  53.  
  54. void solve() {
  55.     int n;
  56.     cin >> n;
  57.     vector <string> vec;
  58.     for (int i = 11; i <= 99; i++) {
  59.         string x = to_string(i);
  60.         if (isInteresting(x)) {
  61.             vec.push_back(x);
  62.         }
  63.     }
  64.     for (int i = 0; vec.size() <= n; i++) {
  65.         string s = vec[i];
  66.         for (char d = '1'; d <= '9'; d++) {
  67.             if (isInteresting(s + d)) {
  68.                 vec.push_back(s + d);
  69.             }
  70.         }
  71.     }
  72.     cout << vec[n - 1] << endl;
  73. }
  74.  
  75. int main() {
  76.     ios::sync_with_stdio(0); cin.tie(0);
  77.     solve();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment