Cod sursa(job #734992)

Utilizator Theorytheo .c Theory Data 15 aprilie 2012 14:23:20
Problema Numere 2 Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.8 kb
#include<fstream>
#include<string.h>
#define w 102
using namespace std;
ifstream fin("numere2.in");
ofstream fout("numere2.out");
typedef int Huge[211];
Huge B,U,P, X,g, K,M;
int i, n , m;
char s[122];
int compara2(Huge,Huge);
void scad(Huge A,Huge B)
{
    int i, T=0;
    for(i = 1 ; i<= B[0] ; ++i)
    {
        A[i]-= B[i];
    }
    for(i = 1; i<= A[0]; i++)
        if(A[i] < 0 )
        {
            A[i]+=10;
            A[i+1]--;
        }
    while(A[A[0]]==0)   A[0]--;

}
int compara(Huge A,Huge B)
{
    int i;
    if(A[0] > B[0])
        return 1;
    if(A[0] < B[0])
        return -1;
    for(i = A[0] ; i>0  ; i --)
    {
        if(A[i] > B[i])
            return 1;
        if(A[i] < B[i])
            return -1;

    }
    return 0;

}
void imp(Huge A, long x)
{
    int R = 0,i;
    for(i = A[0] ;i ; i--)
    {
        A[i] = (R = R * 10 + A[i]) /x;
        R %= x;
    }
    while(A[A[0]] == 0)
        A[0]--;
}
void afis(Huge M)
{
    int i;
    for(i= M[0]; i> 0 ;--i)
        fout <<M[i];
}
void add(Huge A,Huge B)
{
    int i = 0, t = 0;
    for( i = 1 ; i <= A[0] || i <= B[0] || t; i++, t/=10)
        A[i] = (t += A[i] + B[i])% 10;
    A[0] = i - 1;
}
void inm(Huge A)
{
    int i,T = 0,j ;

    K[0] = A[0] + B[0] - 1 ;
    for(i = 1; i <=A[0] ;i++)
        for(j= 1; j <= B[0] ; j++)
            K[i + j - 1] += A[i] *B[j];
    for(i = 1 ; i<= K[0] ;i ++)
    {
        T = (K[i] += T) /10;
        K[i] %= 10;
    }
    if(T)
    K[++K[0] ] = T;
}
int merge(Huge M,int z)
{
    int i = 0,j;
    for(i = 1 ;i <= w; i++)
       K[i] = 0;
    K[0] = 1;
    for(i = 1 ;i <= w; i++)
       B[i] = 0;
       B[1] = 1;
    B[0] = 1;
    for( i = 1; i <= z  ; i ++)
    {
        inm(M);

        for(j = 0 ;j <= w; j++)
        {
            B[j] = K[j]; K[j] = 0;
        }
            K[0] = 1;



    if(compara(B,X)>0)
    return 1;
   // afis(B); fout<<'\n';
    }
  //  afis(B);fout<<'\n'; afis(X);
     return compara(B,X);

}
int compara2(Huge P, Huge U)
{
    int i;
    if(P[0] > U[0])
        return 0;
    if(P[0] < U[0])
        return 1;
    for( i = P[0] ; i > 0 ; i--)
    {
        if(P[i] > U[i])
            return 0;
        if(P[i] < U[i])
            return 1;
    }
    return 1;
}
void pune(Huge A,Huge B)
{
    int i;
    for(i= 0 ;i <= B[0] ; i++)
        A[i] = B[i];
    for(i = B[0] + 1 ;i < w; i++)
        A[i] = 0;
}
int bs(int z)
{
    int i;
    g[0] = g[1] = 1;
    P[0] = P[1] = 1;
    for(i = 2; i <w; i++)
    {
        P[i] = 0;
        U[i] = 0;
    }
    U[1] = 0;
    U[0] = 100;
    U[U[0]] = 1;

    while( compara (P,U) <= 0)
    {

        pune(M,P);
        add(M,U); imp(M,2);
       // afis(M);
         //fout<<'\n' ;

        if( merge (M,z) <= 0)//m^z <= Nr
        {

           add(M,g);
            pune(P,M);
        }

       else
       {
            scad(M,g);
            pune(U,M);
       }
}

      pune(M,P); add(M,U); imp(M,2);
        if(merge(M,z) > 0)
            scad(M,g);
        if(merge(M,z) == 0)
        {
            afis(M);
            return 1;
        }

        return 0;

}
Huge V;
void read()
{
    int i = 1;
    g[0]= g[1] = 1;
    fin.get(s,111,'\n');
    for( i= strlen(s) - 1; i>=0 ;i--)
    {
        X[++X[0]] = s[i] - '0';

    }

   // afis(X); fout <<'\n';
  // fout<<bs(17);
V[0]=2;
V[1]=6;
V[2]=1;
//imp(X,2);
//fout<<compara(V, X);
   // add(X,g);//add(X,g);
    //afis(X);
  //fout << merge(V,13);
   //bs(17);
  //pune( B,X);
  //afis(B);
  //fout <<merge(X,2);
    for( i =68; i  ; i--)
       if( bs(i) ==  1)
       {
           fout<< '\n'<< i;
           break;
       }
}
int main()
{
    read();
    fin.close();
    return 0;

}