Cod sursa(job #1585878)

Utilizator cautionPopescu Teodor caution Data 31 ianuarie 2016 15:59:46
Problema Expresii 2 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>

constexpr int kMaxN = 30;

long long pd[kMaxN+1];

int main()
{
    std::ifstream in("expresii2.in");
    std::ofstream out("expresii2.out");
    int n, k;
    long long p;
    in>>n>>k>>p;
    pd[1] = k;
    for(int i = 2; i <= n; ++i) {
        for(int j = 1; j <= i-2; ++j) pd[i] += pd[j]*pd[n-1-j];
        pd[i] *= 2;
        pd[i] += pd[i-1];
    }
    out<<pd[n]<<'\n';

    int current_case = 1;
    long long aux = pd[n]/k;
    out<<static_cast<char>('A'+(p-1)/aux);
    p = (p-1)%aux+1;
    for(int i = 2; i <= n; ++i) {
        if(current_case == 1) { //we can either add letters or '!'
            //in order for us to add a letter => we will add in fact an (expression)(+/*)<!!..!>
            aux = 0;
            for(int j = 1; j <= n-i; ++j) aux += pd[j];
            aux *= 2;
            if(p <= aux) {
                aux /= k;
                out<<static_cast<char>('A'+(p-1)/aux);
                p = (p-1)%aux+1;
                current_case = 2;
            }
            else {
                out<<'!';
                p -= aux;
            }
        }
        else {
            aux = 0;
            for(int j = 1; j <= n-i-1; ++j) aux += pd[j];
            aux = 2*(2*aux + 1);
            if(p <= aux) {
                aux /= 2;
                if(p <= aux) out<<'+';
                else out<<'*';
                p = (p-1)%aux+1;
                current_case = 1;
            }
            else {
                out<<'!';
                p -= aux;
            }
        }
    }
    return 0;
}