Cod sursa(job #2857982)

Utilizator puica2018Puica Andrei puica2018 Data 26 februarie 2022 19:03:19
Problema Bile2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.7 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("bile2.in");
ofstream fout("bile2.out");

const int MAX_N = 1000 + 5;
const int MAX_SIZE = 120;
const int BASE = 1e9;
const int BASE_DIGITS = 9;

class Huge {
  public:

    int a[MAX_SIZE];
    int size;

    void Reset() {
      for(int i = 0; i < MAX_SIZE; i++) {
        this->a[i] = 0;
      }
      size = 1;
    }

    Huge() {
      Reset();
    }

    void operator = (char* s) {
      Reset(); size = 0;

      for(int i = strlen(s); i > 0; i -= BASE_DIGITS) {
        size++;
        for(int j = std::max(0, i - BASE_DIGITS); j < i; j++) {
          a[size] = a[size] * 10 + s[j] - '0';
        }
      }
    }

    void operator = (long long value) {
      Reset();

      size = 0;
      do {
        a[++size] = value % BASE;
        value /= BASE;
      }while(value != 0);
    }

    void operator = (const Huge &other) {
      this->size = other.size;
      for(int i = 0; i < MAX_SIZE; i++) {
        this->a[i] = other.a[i];
      }
    }

    void operator *= (long long value) {
      long long i, t;
      for(i = 1, t = 0; i <= size || t != 0; i++, t /= BASE) {
        a[i] = (t += 1LL * a[i] * value) % BASE;
      }

      this->size = i - 1;
    }

    void operator /= (long long value) {
      long long i, t;
      for(i = size, t = 0; i > 0; i--) {
        t = t * BASE + a[i];
        a[i] = t / value;
        t %= value;
      }

      while(size > 1 && a[size] == 0) {
        size--;
      }
    }

    void operator += (const Huge &other)  {
      int i, j, t;
      for(i = 1, j = 1, t = 0; i <= this->size || j <= other.size || t != 0; i++, j++, t /= BASE) {
        t += this->a[i] + other.a[i];
        this->a[i] = t % BASE;
      }

      this->size = i - 1;
    }

    void operator -= (const Huge &other) {
      int i, t = 0;
      for(i = 1; i <= this->size; i++) {
        this->a[i] -= (other.a[i] + t);
        t = 0;

        if(this->a[i] < 0) {
          this->a[i] += BASE;
          t = 1;
        }
      }

      while(this->size > 1 && this->a[this->size] == 0) {
        this->size--;
      }
    }

    Huge operator *(const Huge &other) {
      Huge answer;

      for(int i = 1; i <= this->size; i++) {
        long long t = 0;

        int j;
        for(j = 1; j <= other.size || t; j++, t /= BASE) {
          t += answer.a[i + j - 1] + 1LL * this->a[i] * other.a[j];
          answer.a[i + j - 1] = t % BASE;
        }

        answer.size = std::max(answer.size, i + j - 2);
      }

      return answer;
    }

    bool operator >(const Huge &other) const {
      if(this->size != other.size) {
        return this->size > other.size;
      }

      for(int i = this->size; i > 0; i--) {
        if(this->a[i] != other.a[i]) {
          return this->a[i] > other.a[i];
        }
      }

      return false;
    }
};

Huge comb,a,b;
Huge dp[2][MAX_N];

char fa[MAX_SIZE],fb[MAX_SIZE];

bool Check(Huge _a, Huge _b, Huge _c, Huge _d) {
  Huge x=_a*_d;
  Huge y=_b*_c;

  return y>x;
}

int main() {
  long long n,d;
  fin>>n>>d>>fa>>fb;

  a=fa; b=fb;

  Huge c=b; c-=a;
  a=c;


  comb=n;
  for(int i=1; i<=n; i++)
  {
    dp[0][i]=i;
  }

  int now=1,before=0;
  int level=1;

  while(Check(a,b,dp[before][n],comb))
  {
    comb*=(n - level);
    level++;
    comb/=level;

    dp[now][level-1]=(long long)0;
    dp[now][level-2]=(long long)0;

    for(int i=level; i<=n; i++)
    {
      dp[now][i]=dp[now][i-1];

      if(i-d-1>=level-1)
      {
        dp[now][i]+=dp[before][i-d-1];
      }
    }

    now^=1; before^=1;
  }
  fout<<level<<"\n";
  return 0;
}