Cod sursa(job #1737500)

Utilizator cristina_borzaCristina Borza cristina_borza Data 4 august 2016 12:29:37
Problema NextSeq Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <cstring>

#define DIM 100005


using namespace std;

ifstream f ("nextseq.in");
ofstream g ("nextseq.out");

int fn[DIM] , sn[DIM] , unu[DIM] , v[DIM] , qq[DIM];
int n , m , p , x , nr , BASE;

bool comp (int a[] , int b[]) {
    if (a[0] != b[0]) {
        return 0;
    }

    for (int i = 1; i <= a[0]; ++i) {
        if (a[i] != b[i]) {
            return 0;
        }
    }

    return 1;
}

void adun (int a[] , int b[]);

int main() {
    unu[1] = unu[0] = 1;
    f >> n >> m >> p;

    for (int i = 1; i <= n; ++i) {
        f >> x;
        ++v[x];

        BASE = max(BASE , x);
    }

    ++BASE;
    for (int i = m; i >= 1; --i) {
        f >> x;
        fn[i] = x;
    }

    fn[0] = m;
    for (int i = p; i >= 1; --i) {
        f >> x;
        sn[i] = x;
    }

    sn[0] = p;
    adun(fn , unu);

    while (!comp(fn , sn)) {
        adun(fn , unu);
    }

    g << nr - 1;
    return 0;
}

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

        if (v[a[i]] == 0) {
            ok = 0;
        }
    }

    while (t) {
        a[++a[0]] = t % BASE;
        t /= BASE;
    }

    if (ok) {
        ++nr;
    }
}