Cod sursa(job #963884)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 19 iunie 2013 17:12:01
Problema Expresii 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include<stdio.h>
long int n,k,i,j,poz,nv,ll;
long long int p,xx[35][35],sol;
char sc[35],uc;
long long int ss(long int le,long int lp);
void numar();
int main()
{
    FILE *f,*g;f=fopen("expresii2.in","r");g=fopen("expresii2.out","w");
    fscanf(f,"%ld%ld%lld",&n,&k,&p);
    xx[0][0]=0;
    xx[1][1]=0;
    xx[1][0]=k;
    xx[2][2]=0;
    xx[2][1]=1;
    xx[2][0]=k;
    xx[3][3]=0;
    xx[3][2]=2;
    xx[3][1]=2*k+1;
    xx[3][0]=k*xx[3][1];
    for(i=4;i<=n;i++)
    { xx[i][i]=0;
      for(j=i-1;j>1;j--)
       xx[i][j]=k*xx[i][j+1]+2*xx[i-2][j-1]+xx[i-1][j];
      xx[i][1]=k*xx[i][2]+xx[i-1][1];
      xx[i][0]=k*xx[i][1];
    }
    sol=xx[n][0];
    fprintf(g,"%lld\n",sol);
    uc='A';for(i=1;i<k;i++)uc++;
    sc[poz]='A';while(p>xx[n][1]){sc[poz]++;p=p-xx[n][1];}poz++;
    nv=1;ll=n;
    numar();
    fprintf(g,"%s\n",sc);
    fcloseall();
    return 0;
}
void numar()
{
  if(poz==n)return;
  if(poz==n-1)
  { if(nv==1){sc[poz]='!';poz++;numar();return;}
    if(p==1){sc[poz]='+';poz++;numar();return;}
    sc[poz]='*';poz++;numar();return;
  }
  if(nv==1){
         sc[poz]='A';nv++;
         while((sc[poz]<=uc)&&(xx[ll][nv]<p))
         { p-=xx[ll][nv];
           sc[poz]++;
         }
         if(sc[poz]<=uc){poz++;numar();if(poz==n)return;}
         else { sc[poz]='!';poz++;ll--;nv--;numar();if(poz==n)return;}
       }
  sc[poz]='A';nv++;
  while((sc[poz]<=uc)&&(xx[ll][nv]<p))
   { p-=xx[ll][nv];sc[poz]++;}
  if(sc[poz]<=uc){poz++;numar();if(poz==n)return;}
  else
  { nv--;
    sc[poz]='+';nv--;ll-=2;
    if(xx[ll][nv]<p){sc[poz]='*';p-=xx[ll][nv];}
    if(xx[ll][nv]<p){sc[poz]='!';p-=xx[ll][nv];nv++;ll++;poz++;numar();if(poz==n)return;}
    poz++;numar();
  }
}