Cod sursa(job #1737476)

Utilizator cristina_borzaCristina Borza cristina_borza Data 4 august 2016 11:55:15
Problema NextSeq Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <fstream>
#include <cstring>

#define NRCIF 4
#define BASE 10000
#define DIM 1000


using namespace std;

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

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

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[]);
void verif (int a[]);

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

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

    for (int i = m; i >= 1; --i) {
        f >> x;

        int pos = i / NRCIF + (i % NRCIF != 0);
        fn[pos] = fn[pos] * 10 + x;
    }

    fn[0] = m / NRCIF + (m % NRCIF != 0);
    aux[0] = fn[0];

    for (int i = 1; i <= fn[0]; ++i) {
        aux[i] = fn[i];
    }

    for (int i = p; i >= 1; --i) {
        f >> x;

        int pos = i / NRCIF + (i % NRCIF != 0);
        sn[pos] = sn[pos] * 10 + x;
    }

    sn[0] = p / NRCIF + (p % NRCIF != 0);
    adun(aux , unu);

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

    g << nr;
    return 0;
}

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] = a[i] + b[i] +t;
        t = a[i] / BASE;
        a[i] %= BASE;
    }

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


void verif (int a[]) {
    memset(qq , 0 , sizeof(qq));
    int cx = a[1];
    while (cx) {
        qq[cx % 10] = 1;
        cx /= 10;
    }

    for (int i = 2; i <= a[0]; ++i) {
        int cx = a[i];
        for (int j = 1; j <= NRCIF; ++j){
            qq[cx % 10] = 1;
            cx /= 10;
        }
    }

    for (int i = 0; i <= 9; ++i) {
        if (qq[i] > v[i]) {
            return;
        }
    }

    ++nr;
}