Cod sursa(job #1798857)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 5 noiembrie 2016 14:54:11
Problema Pavare2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;

ifstream in ( "pavare2.in"  );
ofstream out( "pavare2.out" );

const int EXP = 3e1 + 3;
const int DIM = 1e2 + 5;
const int MOD = 1e9 + 7;

int k[EXP], ans[EXP], bl[DIM][DIM][EXP], wh[DIM][DIM][EXP];

void add( int a[EXP], int b[EXP] ) {
    int i, t = 0;
    for( i = 1; i <= a[0] || i <= b[0] || t; i ++, t /= 10 ) {
        a[i] = ( t += a[i] + b[i] ) % 10; }
    a[0] = i - 1; return; }

void sub( int a[EXP], int b[EXP] ) {
    int i, t = 0;
    for( i = 1; i <= a[0]; i ++ ) {
        a[i] -= ( ( i <= b[0] ) ? b[i] : 0 ) + t;
        a[i] += ( t = ( a[i] < 0 ) ) * 10; }
    while( a[ a[0] ] == 0 ) { a[ a[0] -- ] = 0; }
    return; }

bool cmp( int a[EXP], int b[EXP] ) {
    if( a[0] != b[0] ) return a[0] > b[0];
    for( int i = a[0]; i >= 1; i -- ) {
        if( a[i] != b[i] ) return a[i] > b[i]; }
    return false; }

int main( int argc, const char *argv[] ) {
    ios::sync_with_stdio( false );
    int n, a, b; in >> n >> a >> b;
    string s; in >> s; k[0] = s.length();
    for( int i = 0; i < s.length(); i ++ ) {
        k[s.length() - i] = s[i] - '0'; }
    wh[1][1][0] = wh[1][1][1] = 1;
    bl[1][1][0] = bl[1][1][1] = 1;
    for( int i = 2; i <= n; i ++ ) {
        for( int j = 1; j <= a; j ++ ) {
            add( bl[i][1], wh[i - 1][j] );
            add( wh[i][j], wh[i - 1][j - 1] ); }
        for( int j = 1; j <= b; j ++ ) {
            add( wh[i][1], bl[i - 1][j] );
            add( bl[i][j], bl[i - 1][j - 1] ); } }
    for( int i = 1; i <= n; i ++ ) {
        for( int j = 2; j <= a; j ++ ) {
            add( wh[i][j], wh[i][j - 1] ); }
        for( int j = 2; j <= b; j ++ ) {
            add( bl[i][j], bl[i][j - 1] ); } }
    add( ans, wh[n][a] ); add( ans, bl[n][b] );
    for( int i = ans[0]; i >= 1; i -- )
        out << ans[i];
    out << endl;
    int l, nr;
    if( cmp( k, wh[n][a] ) == false ) { out << 0; l = 0; nr = 1; }
    else { sub( k, wh[n][a] ); out << 1; l = 1; nr = 1; }
    for( int i = n - 1; i >= 1; i -- ) {
        if( l == 0 && nr == a ) { out << 1; l = 1; nr = 1; } else
        if( l == 1 && nr == b ) { out << 0; l = 0; nr = 1; } else
        if( l == 0 ) {
            if( cmp( k, wh[i][a - nr] ) == 0 ) { out << 0; nr ++; }
            else { sub( k, wh[i][a - nr] ); out << 1; l = 1; nr = 1; } }
        else {
            if( cmp( k, wh[i][a] ) == 0 ) { out << 0; l = 0; nr = 1; }
            else { sub( k, wh[i][a] ); out << 1; nr ++; } } }
    out << endl;
    return 0; }