Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. const int MAXA = 500 * 1000 + 5;
  8. int lDiv[MAXA], ans = MAXA * 10;
  9. vector<int> prDiv[MAXA], pr;
  10.  
  11. void gen(int x, vector<int> a, int res) {
  12.     bool ok = true;
  13.     for(size_t i = 0; i < a.size(); i++) {
  14.         res += a[i] / x;
  15.         a[i] %= x;
  16.         if(a[i])
  17.             ok = false;
  18.     }
  19.     if(ok) {
  20.         ans = min(ans, res);
  21.         return;
  22.     }
  23.     if(res >= ans)
  24.         return;
  25.     for(size_t i = 0; i < prDiv[x].size(); i++)
  26.         gen(x / prDiv[x][i], a, res);
  27. }
  28.  
  29. int main() {
  30.     ios_base::sync_with_stdio(false);
  31.     cin.tie(0);
  32.     srand(time(0));
  33.     for(int i = 2; i < MAXA; i++) {
  34.         if(!lDiv[i]) {
  35.             lDiv[i] = i;
  36.             pr.push_back(i);
  37.         }
  38.         for(size_t j = 0; j < pr.size() && pr[j] <= lDiv[i]; j++) {
  39.             int p = pr[j] * i;
  40.             if(p >= MAXA)
  41.                 break;
  42.             lDiv[p] = pr[j];
  43.         }
  44.     }
  45.     for(int i = 2; i < MAXA; i++) {
  46.         int x = i;
  47.         while(x > 1) {
  48.             int y = lDiv[x];
  49.             prDiv[i].push_back(y);
  50.             while(!(x % y))
  51.                 x /= y;
  52.         }
  53.     }
  54.     int n = 7;
  55.     cin >> n;
  56.     vector<int> a(n);
  57.     int maxa = 0;
  58.     for(int i = 0; i < n; i++) {
  59.         cin >> a[i];
  60.         maxa = max(maxa, a[i]);
  61.     }
  62.     for(int i = max(maxa / 2, 1); i <= maxa; i++)
  63.         gen(i, a, 0);
  64.     cout << ans << '\n';
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement