fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isPrime(int x) {
  5. if (x < 2) return false;
  6. for (int i = 2; i * i <= x; i++)
  7. if (x % i == 0) return false;
  8. return true;
  9. }
  10.  
  11. int main() {
  12. ios::sync_with_stdio(false);
  13. cin.tie(nullptr);
  14.  
  15. string n;
  16. long long Y;
  17. cin >> n >> Y;
  18.  
  19. vector<int> cnt(10, 0);
  20. long long S = 0;
  21. for (char c : n) {
  22. cnt[c - '0']++;
  23. S += c - '0';
  24. }
  25.  
  26. long long A = 0, tmp = Y;
  27. while (tmp) {
  28. A += tmp % 10;
  29. tmp /= 10;
  30. }
  31.  
  32. double x = 21.268443;
  33. double y = 105.204557;
  34.  
  35. double x0 = S + (A % 10);
  36.  
  37. string best = "";
  38.  
  39. for (int a = 9; a >= 0; a--) {
  40. if (cnt[a] == 0) continue;
  41. cnt[a]--;
  42.  
  43. for (int b = 9; b >= 0; b--) {
  44. if (cnt[b] == 0) continue;
  45. cnt[b]--;
  46.  
  47. int R = 10 * b + a;
  48. double y0 = (R + A) % 100;
  49.  
  50. double d = sqrt((x0 - x)*(x0 - x) + (y0 - y)*(y0 - y));
  51. int K = (int)d;
  52.  
  53. if (isPrime(K)) {
  54. string res = "";
  55. res += char('0' + a);
  56. res += char('0' + b);
  57.  
  58. for (int d = 9; d >= 0; d--) {
  59. res += string(cnt[d], char('0' + d));
  60. }
  61.  
  62. if (res[0] != '0') {
  63. cout << res;
  64. return 0;
  65. }
  66. }
  67.  
  68. cnt[b]++;
  69. }
  70.  
  71. cnt[a]++;
  72. }
  73.  
  74. cout << -1;
  75. }
  76.  
Success #stdin #stdout 0.01s 5248KB
stdin
Standard input is empty
stdout
-1