# Cod sursa(job #428044)

Utilizator Data 28 martie 2010 18:47:35 Expresii 2 100 cpp done Arhiva de probleme 1.55 kb
``````#include <cstdio>
#include <cstring>

int N, K;
long long V[64][64], P;

{
scanf("%d %d %lld", &N, &K, &P);
}

void solve()
{
V[0][0] = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j <= i + 1; j++)
{
if (i && !j)
continue;
if (j)
V[i + 1][j - 1] += V[i][j] * K;
if (i != N - 1)
{
if (!j)
V[i + 1][j + 1] += V[i][j];
else
V[i + 1][j] += V[i][j];
}
if (j)
V[i + 1][j + 1] += 2 * V[i][j];
else
V[i + 1][j + 2] += 2 * V[i][j];
}

printf("%lld\n", V[N][0]);
}

void getexpr()
{
char p[64];
int nrv = 0, ind = 0;

memset(p, 0, sizeof(p));
V[0][1] = 1;
for (int i = 0; i < N; i++)
{
int k;
for (k = 'A'; k - 'A' < K; k++)
{
P -= V[N - i - 1][nrv + 1];
if (P <= 0)
{
P += V[N - i - 1][nrv + 1];
break;
}
}

if (k - 'A' < K)
{
p[ind++] = k;
nrv++;
}
else
{
int flag = 0;

if (nrv >= 2)
{
P -= V[N - i - 1][nrv - 1];
if (P <= 0)
{
P += V[N - i - 1][nrv - 1];
p[ind++] = '+';
nrv--;
flag = 1;
}
}

if (flag)
continue;
if (nrv >= 2)
{
P -= V[N - i - 1][nrv - 1];
if (P <= 0)
{
P += V[N - i - 1][nrv - 1];
p[ind++] = '*';
nrv--;
flag = 1;
}
}

if (flag)
continue;
p[ind++] = '!';
}
}

printf("%s\n", p);
}

int main()
{
freopen("expresii2.in", "r", stdin);
freopen("expresii2.out", "w", stdout);