Cod sursa(job #2071081)

Utilizator tgm000Tudor Mocioi tgm000 Data 20 noiembrie 2017 11:04:17
Problema Lampa Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include<cstdio>
#include<cstring>
char s[3027198];
char a[3027198],b[3027198],aux[3027198];
void euclid(int a,int b,int &x,int &y){
   if(b==0){
      x=1;
      y=0;
   }else{
      int x0,y0;
      euclid(b,a%b,x0,y0);
      x=y0;
      y=x0-(a/b)*y0;
   }
}
int main(){
   int n,m,x,y,aa,bb,c,i,xs,ys;
   freopen("lampa.in","r",stdin);
   freopen("lampa.out","w",stdout);
   scanf("%d%d\n",&n,&m);
   scanf("%s",s);
   aa=bb=1;
   for(i=3;i<n;i++){
      c=aa+bb;
      aa=bb;
      bb=c;
   }
   euclid(aa,bb,x,y);
   x*=m;
   y*=m;
   if(x<=0){
      while(x<=0){
         x+=bb;
         y-=aa;
      }
      while(y>0){
         strcpy(a,s);
         a[x]=NULL;
         strcpy(b,s+x);
         b[y]=NULL;
         for(i=3;i<=n;i++){
            strcat(a,b);
            strcpy(aux,a);
            strcpy(a,b);
            strcpy(b,aux);
         }
         if(strcmp(b,s)==0){
            strcpy(a,s);
            a[x]=NULL;
            strcpy(b,s+x);
            b[y]=NULL;
            printf("%s\n%s",a,b);
            return 0;
         }
         x+=bb;
         y-=aa;
      }
      printf("0");
   }else if(y<=0){
      while(y<=0){
         y+=aa;
         x-=bb;
      }
      xs=-1;
      while(x>0){
         strcpy(a,s);
         a[x]=NULL;
         strcpy(b,s+x);
         b[y]=NULL;
         for(i=3;i<=n;i++){
            strcat(a,b);
            strcpy(aux,a);
            strcpy(a,b);
            strcpy(b,aux);
         }
         if(strcmp(b,s)==0){
            xs=x;
            ys=y;
         }
         y+=aa;
         x-=bb;
      }
      if(xs==-1)
         printf("0");
      else{
         strcpy(a,s);
         a[xs]=NULL;
         strcpy(b,s+xs);
         b[ys]=NULL;
         printf("%s\n%s",a,b);
      }
   }
   return 0;
}