Cod sursa(job #851787)

Utilizator stoicatheoFlirk Navok stoicatheo Data 10 ianuarie 2013 14:28:39
Problema Lampa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include<cstdio>
#include<cstring>
#include<cstdlib>
const int N=1<<16;
char s[1<<22];
int lung,n,m,v[2],f[2],ch[2][2],fib[30],c[2][30];
bool ok;
void read()
{
    freopen("lampa.in","r",stdin);
    freopen("lampa.out","w",stdout);
    scanf("%d%d\n",&n,&m);
    gets(s+1);
}
void fibo(int n)
{
    lung=0;
    ++lung;
    c[1][lung]=1;
    c[0][lung]=0;
    ++lung;
    c[1][lung]=0;
    c[0][lung]=1;
    fib[1]=1;
    for(int j=2;j<=n;j++)
    {
        fib[j]=fib[j-1]+fib[j-2];
        ++lung;
        c[1][lung]=fib[j-1];
        c[0][lung]=fib[j];
    }
}
void get(int conc,int pi,int pf,int runda)
{
    if(pf-pi+1==v[conc])
    {
        if(!f[conc])
        {
            ch[conc][0]=pi;
            ch[conc][1]=pf;
            f[conc]=true;
        }
        else
        {
            int q=ch[conc][0]-1;
            for(int i=pi;i<=pf;i++)
                if(s[i]!=s[++q])
                {
                    ok=false;
                    break;
                }
        }
        return;
    }
    if(pf-pi+1<v[conc])
    {
        ok=false;
        return;
    }
    if(runda==1)
    {
        ok=false;
        return;
    }
    if(runda>2 && ok)
        get(conc,pi,pi+v[conc]*c[conc][runda-2]+v[1-conc]*c[1-conc][runda-2]-1,runda-2);
    if(ok && runda>1)
        get(1-conc,pi+v[conc]*c[conc][runda-2]+v[1-conc]*c[1-conc][runda-2],pf,runda-1);
}
void afis()
{
    for(int i=1;i>=0;i--)
    {
        for(int j=ch[i][0];j<=ch[i][1];j++)
            printf("%c",s[j]);
        printf("\n");
    }
}
void solve()
{
    int b=c[0][n],a=c[1][n];
    int lim=(m/a)+1;
    for(int x=1;x<=lim;x++)
        if((m-a*x)%b==0 && m-a*x>0)
        {
            int y=(m-a*x)/b;
            v[0]=y;
            v[1]=x;
            f[0]=f[1]=false;
            ok=true;
            ch[0][0]=ch[0][1]=ch[1][0]=ch[1][1]=0;
            get(n%2,1,m,n);
            if(ok)
            {
                afis();
                exit(0);
            }
        }
    printf("0\n");
}
int main()
{
    read();
    fibo(25);
    solve();
    return 0;
}