Cod sursa(job #2003674)

Utilizator robx12lnLinca Robert robx12ln Data 23 iulie 2017 16:42:04
Problema Lampa Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include<fstream>
#include<string>
using namespace std;
ifstream fin("lampa.in");
ofstream fout("lampa.out");
string s, aux, a, b;
int m, n, D[2][30];
int vf( int L0, int L1 ){
    a.clear();
    b.clear();
    int p0 = 0, p1 = 0;
    if( n % 2 == 0 )
        p0 = L1;
    else
        p1 = L0;
    a = s.substr( p0, L0 );
    b = s.substr( p1, L1 );
    int p = 0;
    for( int poz = 0; poz < aux.length(); poz++ ){
        if( aux[poz] == '0' ){
            if( s.compare( p, L0, a, 0, L0 ) != 0 )
                return 0;
            p += L0;
        }else{
            if( s.compare( p, L1, b, 0, L1 ) != 0 )
                return 0;
            p += L1;
        }
    }
    return 1;
}
int main(){
    fin >> n >> m >> s;
    D[0][1] = D[1][2] = 1;
    for( int i = 3; i <= n; i++ ){
        D[0][i] = D[0][i - 1] + D[0][i - 2];
        D[1][i] = D[1][i - 1] + D[1][i - 2];
    }
    if( D[0][n] + D[1][n] > m ){
        fout << "0\n";
        return 0;
    }
    a = '0';
    b = '1';
    for( int i = 3; i <= n; i++ ){
        aux = a + b;
        a = b;
        b = aux;
    }
    for( int lg2 = m / D[1][n]; lg2 >= 1; lg2-- ){
        int lg1 = ( m - lg2 * D[1][n] ) / D[0][n];
        if( D[1][n] * lg2 + D[0][n] * lg1 == m && vf( lg1, lg2 ) == 1 ){
            fout << a << "\n" << b << "\n";
            return 0;
        }
    }
    return 0;
}