Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. #include <iomanip>
  13. #include <queue>
  14. #include <utility>
  15. #include <ctime>
  16. #include <numeric>
  17.  
  18. #define pb push_back
  19. #define mp make_pair
  20. #define x first
  21. #define y second
  22. #define forn(i, n) for (i = 0; i < n; i++)
  23.  
  24. using namespace std;
  25.  
  26. typedef pair <int, int> pii;
  27. typedef long double ld;
  28. typedef long long ll;
  29. typedef pair <ll, ll> pll;
  30.  
  31. const ld EPS = 1e-9;
  32. const int INF = (int) 1e9;
  33. const int N = (int) 1e5 + 5;
  34. const ll M = (int) 1e9 + 7;
  35.  
  36. int s(int x) {
  37.     if (x < 10)
  38.         return x;
  39.     int sum = 0;
  40.     while (x)
  41.         sum += x % 10, x /= 10;
  42.     return s(sum);
  43. }
  44.  
  45. pll a[N];
  46. bool used[N];
  47. vector <ll> ans;
  48.  
  49. int main() {
  50.     int n;
  51.     cin >> n;
  52.     for (int i = 0; i < n; i++) {
  53.         cin >> a[i].y;
  54.         a[i].x = s(a[i].y);
  55.     }
  56.     sort(a, a + n);
  57.     for (int i = 0; i < n; i++)
  58.         if (a[i].x == 3 && !used[i])
  59.             for (int j = i + 1; j < n; j++)
  60.                 if (!used[j] && a[j].x == 3) {
  61.                     ans.pb(a[i].y * a[j].y), used[i] = used[j] = 1;
  62.                     break;
  63.                 }
  64.     for (int i = 0; i < n; i++)
  65.         if (a[i].x == 2 && !used[i])
  66.             for (int j = 0; j < n; j++)
  67.                 if (!used[j] && a[j].x == 4) {
  68.                     ans.pb(a[i].y * a[j].y), used[i] = used[j] = 1;
  69.                     break;
  70.                 }
  71.     for (int i = 0; i < n; i++)
  72.         for (int j = 0; j < n; j++)
  73.             for (int k = 0; k < n; k++)
  74.                 if (!used[i] && !used[j] && !used[k] && a[i].x == 2 && a[j].x == 2 && a[k].x == 2 && i != j && i != k && j != k)
  75.                     ans.pb(a[i].y * a[j].y * a[k].y), used[i] = used[j] = used[k] = 1;
  76.     for (int i = 0; i < n; i++)
  77.         if (a[i].x == 3 && !used[i])
  78.             for (int j = 0; j < n; j++)
  79.                 if (!used[j] && a[j].x == 2) {
  80.                     ans.pb(a[i].y * a[j].y), used[i] = used[j] = 1;
  81.                     break;
  82.                 }
  83.     for (int i = 0; i < n; i++)
  84.         if (!used[i])
  85.             ans.pb(a[i].y);
  86.     cout << ans.size() << endl;
  87.     for (int i = 0; i < (int)ans.size(); i++)
  88.         cout << ans[i] << ' ';
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement