Cod sursa(job #1725974)

Utilizator Tudor_CandeaCandea Tudor Tudor_Candea Data 6 iulie 2016 21:00:46
Problema Factorial Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>

using namespace std;
ifstream fin ("fact.in");
ofstream fout ("fact.out");

long long p;

int main()
{
    int n=6, n1=0, v=25, v1=1, x, x1, x2, v2;
    fin>>p;

    while(p>n)
    {
        n1=n;
        n=5*n+1;
        v1=v;
        v=v*5;
    }
    x2=n-1;
    v2=v;

    if(p==n)
        fout<<v;
    else
    {
        while(n!=p and n1!=p)
        {
            x2=x2/5;
            v2=v2/5;
            x=x2;
            x1=v2;
            n=n-1;
            while(n!=(n1+x))
            {
                n1=n1+x;
                v1=v1+x1;
                n=n-x;
                v=v-x1;
                if(n1>p)
                {
                    n1=n1-x;
                    v1=v1-x1;
                }
                if(n<p)
                {
                    n=n+x;
                    v=v+x1;
                }
                if((n-1)==p)
                {
                    fout<<-1;
                    break;
                }
                if(n==p)
                {
                    fout<<v;
                    break;
                }
                if(n1==p)
                {
                    fout<<v1;
                    break;
                }

            }
            if((n-1)==p)
            {
                    break;
            }
        }
    }
return 0;
}