Advertisement
Dang_Quan_10_Tin

MAXPRIME TS10 PTNK 2018-2019

Jan 6th, 2022
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #define task "MAXPRIME"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. string s;
  14.  
  15. void Read()
  16. {
  17.     cin >> s;
  18. }
  19.  
  20. // String to Integer
  21. ll toi(const string &s)
  22. {
  23.     ll ans(0);
  24.     for (auto i : s)
  25.         ans = ans * 10 + i - '0';
  26.     return ans;
  27. }
  28.  
  29. bool IsPrime(ll v)
  30. {
  31.     if (v == 1)
  32.         return false;
  33.     if (v % 2 == 0) // Số chẵn
  34.         return v == 2;
  35.  
  36.     for (int i = 3; 1ll * i * i <= v; i += 2)
  37.         if (v % i == 0)
  38.             return false;
  39.  
  40.     return true;
  41. }
  42.  
  43. void Solve()
  44. {
  45.     vector<int> p;
  46.  
  47.     for (int i = 0; i < (int)s.size(); ++i)
  48.         if (s[i] == '?')
  49.             p.emplace_back(i);
  50.  
  51.     string ans;
  52.  
  53.     for (int i = 1; i < 10; ++i)
  54.         for (int j = 1; j < 10; ++j)
  55.         {
  56.             s[p[0]] = i + '0';
  57.             s[p[1]] = j + '0';
  58.  
  59.             if (IsPrime(toi(s)))
  60.                 ans = max(ans, s);
  61.         }
  62.  
  63.     cout << ans;
  64. }
  65.  
  66. int32_t main()
  67. {
  68.     ios::sync_with_stdio(0);
  69.     cin.tie(0);
  70.     cout.tie(0);
  71.     if (fopen(task ".INP", "r"))
  72.     {
  73.         freopen(task ".INP", "r", stdin);
  74.         freopen(task ".OUT", "w", stdout);
  75.     }
  76.  
  77.     Read();
  78.     Solve();
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement