Cod sursa(job #235948)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 26 decembrie 2008 13:29:37
Problema Lampa Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 3.84 kb
#include <stdio.h>
#include <string.h>

int fib[22]={1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6756,
             10946,17702,28648};
int N,M;
int x,i,j,nr;
char s[5000];
char s1[5000];
char s2[5000];
char s3[5000];
char s4[5000];
char s5[5000];
int x1,x2;
int c,ok,nrx,ok1;



int find(int a, int b)
{
    return (M-a*x1)/b;
}
    

int main()
{
    freopen("lampa.in","r",stdin);
    scanf("%d %d\n", &N,&M);
    for (i=1;i<=M;++i)
         scanf("%c", &s[i]);
    x1=N;     
    freopen("lampa.out","w",stdout);     
    
    nr=3;
    while (nr<=x1)
    {
      x=find(fib[nr-2],fib[nr-1]);
      //x2=x;
      //printf("%d\n",x);
      if (M==x1*fib[nr-2]+fib[nr-1]*x)
                if (N%2==0)
                    {
                        //printf("A intrat %d\n", x);
                         x2=x;
                         for (i=1;i<=N;++i)
                         {
                         s2[i]=s[i];
                         s4[i]=s[i];
                         }
                         for (j=N+1;j<=N+x;++j)
                         {
                         s1[j-N]=s[j];
                         s3[j-N]=s[j];
                         }
                         c=N+x;
                         for (i=3;i<x1;++i)
                              {
                                    for (j=1;j<=N;++j)
                                       // {
                                            s5[j]=s3[j];
                                      //      printf("%c", s5[j]);
                                      //  }
                                    for (j=N+1;j<=c;++j)
                                       // {
                                            s5[j]=s4[j-N];
                                      //      printf("%c", s5[j]);
                                      //  }
                                      //  printf("\n");
                                        
                                    for (j=1;j<=x;++j)
                                      //  {
                                            s3[j]=s4[j];
                                       //     printf("%c", s3[j]);
                                      //  }
                                      //  printf("\n");
                                    N=x;
                                    for (j=1;j<=c;++j)
                                     //   {
                                        s4[j]=s5[j];
                                    //    printf("%c", s4[j]);
                                    //    }
                                    x=c;
                                    c=N+x;
                              }
                          for (j=1;j<=N;++j)
                                 s5[j]=s3[j];
                          for (j=N+1;j<=c;++j)
                                 s5[j]=s4[j-N];
                          
                          ok1=0;
                          for (i=1;i<=M;++i)
                                if (s5[i]==s[i])
                                     ok1=1;
                                     else
                                     {ok1=0;
                                     break;}
                          
                          if (ok1)
                              {
                                    for (i=1;i<=x1;++i)
                                    printf("%c", s1[i]);
                                    printf("\n");
                                    for (i=1;i<=x2;++i)
                                    printf("%c", s2[i]);
                                    ok=1;
                                    break;
                                    
                              }
                }
         if (ok1) break;
         nr++;
        }
      if (!ok) printf("0\n");  
return 0;
}