Dang_Quan_10_Tin

CAU3 HSGHN L9 2014-2015

Dec 25th, 2021 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #define task "CAU3"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. constexpr int N = 4e5 + 5;
  10. int t, m(0), id[15];
  11. string s[15], res[N];
  12.  
  13. void Read()
  14. {
  15.     cin >> t;
  16. }
  17.  
  18. // Tương đương hàm to_string
  19. string Get(int v)
  20. {
  21.     string ans;
  22.  
  23.     while (v > 0)
  24.     {
  25.         ans.push_back(char(v % 10 + '0'));
  26.         v /= 10;
  27.     }
  28.  
  29.     reverse(ans.begin(), ans.end());
  30.     return ans;
  31. }
  32.  
  33. void Solve()
  34. {
  35.     // Phân tích nguyên tố
  36.     for (int i = 2; i * i <= t; ++i)
  37.     {
  38.         string tmp = Get(i);
  39.         if (t % i == 0)
  40.         {
  41.             ++m;
  42.             while (t % i == 0)
  43.             {
  44.                 t /= i;
  45.                 // Do các số giống nhau nằm liền nhau
  46.                 s[m] += tmp;
  47.             }
  48.         }
  49.     }
  50.     // Nếu t có ước nguyên tố lớn hơn căn t
  51.     if (t != 1)
  52.     {
  53.         ++m;
  54.         s[m] = Get(t);
  55.     }
  56.  
  57.     // Duyệt hoán vị
  58.  
  59.     for (int i = 1; i <= m; ++i)
  60.         id[i] = i;
  61.  
  62.     string ans;
  63.  
  64.     do
  65.     {
  66.         string tmp;
  67.         for (int i = 1; i <= m; ++i)
  68.             tmp += s[id[i]];
  69.  
  70.         ans = max(ans, tmp);
  71.     } while (next_permutation(id + 1, id + m + 1));
  72.  
  73.     cout << ans;
  74. }
  75.  
  76. int32_t main()
  77. {
  78.     ios::sync_with_stdio(0);
  79.     cin.tie(0);
  80.     cout.tie(0);
  81.     if (fopen(task ".INP", "r"))
  82.     {
  83.         freopen(task ".INP", "r", stdin);
  84.         freopen(task ".OUT", "w", stdout);
  85.     }
  86.  
  87.     Read();
  88.     Solve();
  89. }
  90.  
Add Comment
Please, Sign In to add comment