Cod sursa(job #1085661)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 17 ianuarie 2014 10:47:56
Problema Bile2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <cstdio>
#include <algorithm>
#include <cstring>

#define NMAX 1007
#define BASE 100000

using namespace std;

int D[2][NMAX][NMAX], C[2][NMAX][NMAX];

void adun(int a[], int b[]){
    int t = 0;
    a[0] = max(a[0], b[0]);
    for(int i = 1; i <= a[0]; ++i){
        a[i] += b[i] + t;
        t = a[i] / BASE;
        a[i] %= BASE;
    }
    while(t > 0){
        a[++a[0]] = t % BASE;
        t /= BASE;
    }
}

void scad(int a[], int b[]){
    int t = 0;
    for(int i = 1; i <= a[0]; ++i){
        a[i] -= (b[i] + t);
        if(a[i] < 0)
            t = 1;
        else
            t = 0;
        a[i] += t * BASE;
    }
    while(a[a[0]] == 0 && a[0] > 0)
        -- a[0];
}

void prod(int a[], int x){
    int t = 0;
    for(int i = 1; i <= a[0]; ++i){
        a[i] *= x;
        a[i] += t;
        t = a[i] / BASE;
        a[i] %= BASE;
    }
    while(t > 0){
        a[++a[0]] = t % BASE;
        t /= BASE;
    }
}

inline int Comp(int a[], int b[]){
    if(a[0] > b[0])
        return 1;
    if(a[0] < b[0])
        return -1;
    for(int i = a[0]; i >= 1; --i)
        if(a[i] != b[i]){
            if(a[i] > b[i])
                return 1;
            if(a[i] < b[i])
                return -1;
        }
    return 0;
}

int main(){
    int n, d, a, b, Co;
    freopen("bile2.in", "r", stdin);
    freopen("bile2.out", "w", stdout);
    scanf("%d %d %d %d", &n, &d, &a, &b);
    for(int i = 0; i <= n; ++i)
        D[0][i][0] = D[0][i][1] = 1;
    C[0][0][0] = C[0][0][1] = 1;
    for(int i = 1, H = 1; i <= n; ++i, H = 1 - H){
        memset(C[H], 0, sizeof(C[H]));
        C[H][0][0] = C[H][0][1] = 1;
        for(int j = 1; j <= n; ++j){
            memcpy(C[H][j], C[1 - H][j], sizeof(C[H][j]));
            adun(C[H][j], C[1 - H][j - 1]);
        }
        Co = 1 - H;
    }
    int Aux[NMAX], Aux2[NMAX];
    Co = 1 - Co;
    for(int i = 1, H = 1; i <= n; ++i, H = 1 - H){
        memset(D[H], 0, sizeof(D[H]));
        for(int j = 1; j <= n; ++j){
            memcpy(D[H][j], D[H][j - 1], sizeof(D[i][j - 1]));
            adun(D[H][j], D[1 - H][max(j - d - 1, 0)]);
        }
        memset(Aux, 0, sizeof(Aux));
        memcpy(Aux, C[Co][i], sizeof(Aux));
        scad(Aux, D[H][n]);
        prod(Aux, b);

        memset(Aux2, 0, sizeof(Aux));
        memcpy(Aux2, C[Co][i], sizeof(Aux2));
        prod(Aux2, a);

        if(Comp(Aux, Aux2) >= 0){
            printf("%d\n", i);
            return 0;
        }
    }
    return 0;
}