Cod sursa(job #30180)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 13 martie 2007 08:34:28
Problema Expresii 2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <stdio.h>

#define maxx 30
#define ll long long

int n,m;
ll c[maxx][maxx][maxx];
ll x,sol;
int s[maxx];

int max(int a,int b)
{
	if (a>b) return a;
	return b;
}

int min(int a,int b)
{
	if (a<b) return a;
    return b;
}

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

	scanf("%d %d %lld",&n,&m,&x);

	int i,j,k,p,y;

	c[0][1][0]=1;

	for (i=0;i<n;i++)
	{
		  for (j=0;j<=n;j++)
			for (k=0;k<m+3;k++)
			  if (c[i][j][k]!=0)
			  {
				 if (j<n-i) c[i+1][j][m+2]+=c[i][j][k];
				 if ((j>1) || ((j==1) && (i==n-1)))
				   for (p=0;p<m;p++) c[i+1][j-1][p]+=c[i][j][k];

				 if (j<n-i-1)
				 {
					c[i+1][j+1][m]+=c[i][j][k];
					c[i+1][j+1][m+1]+=c[i][j][k];
				 }
			  }
	}

	for (j=0;j<m+2;j++) sol+=c[n][0][j];

	y=0;

	for (i=n;i>0;i--)
		for (j=0;j<m+3;j++)
			if (c[i][y][j]<x) x-=c[i][y][j];
			else {
					 s[n-i+1]=j;
					 if (j<m) y++;
					 else if (j<m+2) y--;
					 j=m+3;
					 break;
				 }

	printf("%lld\n",sol);

	for (i=1;i<=n;i++)
	  if (s[i]<m) printf("%c",'A'+s[i]);
	  else if (s[i]==m) printf("+");
		   else if (s[i]==m+1) printf("*");
				else if (s[i]==m+2) printf("!");

	printf("\n");

	return 0;
}