Cod sursa(job #2253629)

Utilizator MoodyFaresFares Mohamad MoodyFares Data 4 octombrie 2018 11:02:28
Problema Minim2 Scor 0
Compilator cpp Status done
Runda shimulare_fara_shim Marime 1.12 kb
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAX_N = 100000;
const double eps = 1.e-6;

int d[1 + MAX_N];

bool cmp(int a, int b) {
  return a > b;
}

int main() {
  freopen("minim2.in", "r", stdin);
  freopen("minim2.out", "w", stdout);
  
  int n;
  double a, b, rec;
  scanf("%d", &n);
  scanf("%lf%lf%lf", &a, &b, &rec);
  double S = 0;
  double aLaN = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &d[i]);
    S += 1. * d[i];
    aLaN *= a;
  }
  sort(d + 1, d + n + 1, cmp);
  double r = rec / S;
  int ans = 0;
  if (fabs(S * aLaN - rec) <= eps) {
    for (int i = 1; i <= n; i++) {
      S -= 1. * d[i];
      S += 1. * d[i] * a;
      ans++;
      if (fabs(S - rec) <= eps) {
        printf("%d\n", ans);
        return 0;
      }
    }
  }
  r /= aLaN;
  int left = 1;
  int right = 500000000;
  int x = 0;
  while (left <= right) {
    int med = (left + right) >> 1;
    double pb = 1;
    for (int i = 1; i <= med; i++)
      pb *= b;
    if (fabs(pb - r) <= eps) {
      left = med + 1;
      x = med;
    } else {
      right = left - 1;
    }
  }
  printf("%d\n", x + n);
  return 0;
}